![]() |
![]() |
![]() |
||||
Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact us. |
![]() ![]() |
|
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed. |
|
Thread Tools |
![]() |
#1 |
Confirmed User
Industry Role:
Join Date: Jul 2003
Posts: 1,041
|
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
__________________
http://steelehard.com/ الإباحية باللغة العربية Discord: https://discord.gg/QWJpmDSFc2 http://le-beverley.com/ Under Development Twitter: https://twitter.com/steele_hard Spotify : https://open.spotify.com/show/43uaTA1AEPa2Nf0Tmvppd0 |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#2 |
making it rain
Industry Role:
Join Date: Oct 2003
Location: seattle
Posts: 22,037
|
You should ask Publisher Bucks
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#3 | |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,072
|
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. .
__________________
All cookies cleared! |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#4 | |
Confirmed User
Industry Role:
Join Date: Jul 2003
Posts: 1,041
|
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
__________________
http://steelehard.com/ الإباحية باللغة العربية Discord: https://discord.gg/QWJpmDSFc2 http://le-beverley.com/ Under Development Twitter: https://twitter.com/steele_hard Spotify : https://open.spotify.com/show/43uaTA1AEPa2Nf0Tmvppd0 |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#5 | |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,072
|
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. .
__________________
All cookies cleared! |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#6 |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,072
|
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. .
__________________
All cookies cleared! |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#7 |
Confirmed User
Industry Role:
Join Date: Jul 2003
Posts: 1,041
|
I honestly appreciate the help and got it doing something that looks like it's working.
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.
__________________
http://steelehard.com/ الإباحية باللغة العربية Discord: https://discord.gg/QWJpmDSFc2 http://le-beverley.com/ Under Development Twitter: https://twitter.com/steele_hard Spotify : https://open.spotify.com/show/43uaTA1AEPa2Nf0Tmvppd0 |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#8 |
Registered User
Industry Role:
Join Date: Feb 2022
Posts: 40
|
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.
__________________
Cheap VPS/Shared Hosting: RackNerd Discord: Clown#5147 Projects: JuicySluts Website: Clownonymous Other Domains: JuicyBate - HornyFeed - Pimp Ink - Fansily Skills: API/HTML/CSS/JS/PHP/PYTHON |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#9 | |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,072
|
Quote:
I try to remember that everybody learns things differently. .
__________________
All cookies cleared! |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#10 |
Industry Role:
Join Date: Aug 2006
Location: Little Vienna
Posts: 32,235
|
See this: https://www.w3schools.com/php/php_mysql_connect.asp
That is how i learned to use both mysqli and pdo connections. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#11 |
Confirmed User
Industry Role:
Join Date: Jul 2003
Posts: 1,041
|
Don't sweat it... I know that I'm kind of retarded when it comes to this kind of stuff.
__________________
http://steelehard.com/ الإباحية باللغة العربية Discord: https://discord.gg/QWJpmDSFc2 http://le-beverley.com/ Under Development Twitter: https://twitter.com/steele_hard Spotify : https://open.spotify.com/show/43uaTA1AEPa2Nf0Tmvppd0 |
![]() |
![]() ![]() ![]() ![]() ![]() |