![]() |
Advice need on how to fetch data from mysql database using PDO
I'm seriously rusty with this stuff.
I've also bee trying to do the same thing with mysqli_query but no success. So far the only luck I've had is being able to connect to the database |
You should ask Publisher Bucks :1orglaugh
|
Quote:
($db = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf 8",$dbuser,$dbpass,array(PDO::ATTR_EMULATE_PREPARE S => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));) Now you just use the connection. simple query: $stmnt=$db->query('select * from tablename') check for results $stmnt->rowcount() fetch results into an array: $stmnt->fetch(PDO::FETCH_ASSOC) So for a typical table read you have: $db = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf 8",$dbuser,$dbpass,array(PDO::ATTR_EMULATE_PREPARE S => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); $stmnt=$db->query('select * from tablename'); if($stmnt->rowcount()>0) { while($row = $stmnt->fetch(PDO::FETCH_ASSOC)) { do something..... } That is a very simplified view. In a production environment you would be using try...catch on the connect and on the query calls. For the query itself you would probably use a prepare and execute. Your list of pdo functions and constants are at: https://www.php.net/manual/en/book.pdo.php Have fun. . |
Quote:
<!doctype html> <html> <head> <base href="https://gfy.com/" /><!--[if IE]></base><![endif]--></head> <title>mysql test</title> <body> <?php $servername = "localhost"; $username = "root"; $password = "password"; try { $conn = new PDO("mysql:host=$servername;dbname=testing", $username, $password); // set the PDO error mode to Exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } $stmnt=$conn->query('select * from test01') $stmnt->rowcount() $stmnt->fetch(PDO::FETCH_ASSOC) $conn = null; ?> </body> </html> I'm getting this : Parse error: syntax error, unexpected '$stmnt' (T_VARIABLE) in C:\wamp64\www\index2.php on line 20 |
Quote:
The error is because you do not have semi-colons at the end of your statements but once you fix that the code still won't do shit. You need to use the rowcount() inside an if for it to make sense. You also need to attempt to understand what you are writing. . |
1. extablish the mysql cocnnection (this you are doinfg properly)$db = new
2. Perfform an action on the database , again you did this properly but need to end the statement with the semi colon. $stmnt=$conn->(query('select * from test01'); 3. Check to see if you have results: 4. If you have results, do something with those results such as iterate through and echo a field or something. if($stmnt->rowcount()>0) { while($row = $stmnt->fetch(PDO::FETCH_ASSOC)) { do something.....; } Setting the connection to null really doesn't do anything for you imho. The connection will close at the end of the program. . |
Quote:
So I think you managed to push me in the right direction. But... I've got sad news... I am attempting to understand what I'm writing. When everything is all said and done... Let me just say that there are good reasons why I stopped pursing an education and career in anything that couldn't be fixed with hooker or a hammer. |
Always be sure to close our your PHP lines of code with ";" as sarettah said. You miss one little thing and PHP will always bitch at you.
|
Quote:
I try to remember that everybody learns things differently. . |
See this: https://www.w3schools.com/php/php_mysql_connect.asp
That is how i learned to use both mysqli and pdo connections. |
Quote:
|
All times are GMT -7. The time now is 01:19 PM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123