View Single Post
Old 04-15-2022, 12:17 PM  
sarettah
see you later, I'm gone
 
Industry Role:
Join Date: Oct 2002
Posts: 14,091
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 offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote