GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Advice need on how to fetch data from mysql database using PDO (https://gfy.com/showthread.php?t=1354198)

hamiltonsteele 04-15-2022 10:59 AM

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

fuzebox 04-15-2022 11:48 AM

You should ask Publisher Bucks :1orglaugh

sarettah 04-15-2022 12:17 PM

Quote:

Originally Posted by hamiltonsteele (Post 22992146)
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.


.

hamiltonsteele 04-15-2022 12:37 PM

Quote:

Originally Posted by sarettah (Post 22992163)
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

sarettah 04-15-2022 01:08 PM

Quote:

Originally Posted by hamiltonsteele (Post 22992170)
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.

.

sarettah 04-15-2022 01:13 PM

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.

.

hamiltonsteele 04-15-2022 01:44 PM

Quote:

Originally Posted by sarettah (Post 22992179)

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.

Clown 04-15-2022 02:02 PM

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.

sarettah 04-15-2022 02:23 PM

Quote:

Originally Posted by hamiltonsteele (Post 22992189)
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.

.

Klen 04-15-2022 02:23 PM

See this: https://www.w3schools.com/php/php_mysql_connect.asp
That is how i learned to use both mysqli and pdo connections.

hamiltonsteele 04-15-2022 02:39 PM

Quote:

Originally Posted by sarettah (Post 22992199)
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.


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