View Single Post
Old 04-15-2022, 12:37 PM  
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