Basic PDO example

from the Artful MySQL Tips List

In this code, substitute the name of the computer where the MySQL server is running for HOSTNAME, your MySQL username for USR, and your MySQL password for PWD:


try {
echo "Available database drivers:<br />";
var_dumpPDO::getavailableDrivers() );
"<br />";
$pdo = new PDO"mysql:host=HOSTNAME; dbname=t"'USR''PWD' );
$qry "select host, user from mysql.user";
"Results for $qry:<br />";
$pdo->query$qry ) as $row print_r$row );

PDOException $e ) {
"Error: " $e->getMessage() . "<br />" );


Last updated 21 Aug 2012

Return to the Artful MySQL Tips page