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.

Post New Thread Reply

Register GFY Rules Calendar
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 04-15-2022, 10:59 AM   #1
hamiltonsteele
Confirmed User
 
hamiltonsteele's Avatar
 
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
hamiltonsteele is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-15-2022, 11:48 AM   #2
fuzebox
making it rain
 
fuzebox's Avatar
 
Industry Role:
Join Date: Oct 2003
Location: seattle
Posts: 22,037
You should ask Publisher Bucks
fuzebox is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-15-2022, 12:17 PM   #3
sarettah
see you later, I'm gone
 
Industry Role:
Join Date: Oct 2002
Posts: 14,072
Quote:
Originally Posted by hamiltonsteele View Post
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
Ok, you have the connection

($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!
sarettah is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-15-2022, 12:37 PM   #4
hamiltonsteele
Confirmed User
 
hamiltonsteele's Avatar
 
Industry Role:
Join Date: Jul 2003
Posts: 1,041
Quote:
Originally Posted by sarettah View Post
Ok, you have the connection

($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.


.
This is the actual file that I'm working on:

<!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
hamiltonsteele is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-15-2022, 01:08 PM   #5
sarettah
see you later, I'm gone
 
Industry Role:
Join Date: Oct 2002
Posts: 14,072
Quote:
Originally Posted by hamiltonsteele View Post
T
$stmnt=$conn->query('select * from test01')
$stmnt->rowcount()
$stmnt->fetch(PDO::FETCH_ASSOC)
$conn = null;

I'm getting this :

Parse error: syntax error, unexpected '$stmnt' (T_VARIABLE) in C:\wamp64\www\index2.php on line 20
You apparently did not actually read what I wrote. You used tehe statements but ignored the usage.

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!
sarettah is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-15-2022, 01:13 PM   #6
sarettah
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!
sarettah is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-15-2022, 01:44 PM   #7
hamiltonsteele
Confirmed User
 
hamiltonsteele's Avatar
 
Industry Role:
Join Date: Jul 2003
Posts: 1,041
Quote:
Originally Posted by sarettah View Post

You also need to attempt to understand what you are writing.

.
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
hamiltonsteele is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-15-2022, 02:02 PM   #8
Clown
Registered User
 
Clown's Avatar
 
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
Clown is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-15-2022, 02:23 PM   #9
sarettah
see you later, I'm gone
 
Industry Role:
Join Date: Oct 2002
Posts: 14,072
Quote:
Originally Posted by hamiltonsteele View Post
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.
My apologies for sounding harsh. I was frustrated.

I try to remember that everybody learns things differently.

.
__________________
All cookies cleared!
sarettah is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-15-2022, 02:23 PM   #10
Klen
 
Klen's Avatar
 
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.
Klen is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-15-2022, 02:39 PM   #11
hamiltonsteele
Confirmed User
 
hamiltonsteele's Avatar
 
Industry Role:
Join Date: Jul 2003
Posts: 1,041
Quote:
Originally Posted by sarettah View Post
My apologies for sounding harsh. I was frustrated.

I try to remember that everybody learns things differently.

.
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
hamiltonsteele is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks

Tags
database, success, mysqli_query, connect, luck, data, mysql, fetch, pdo, stuff, advice, bee, rusty



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.