Quote:
Originally Posted by sarettah
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