|
Using PDO with SQLlite or Mysql |
|
Example code for using PDO and exception handling.
I just thought I'd try it with SQLite and it's very simple. Uncommenting the second constructor shows how it runs with MySQL.
<?php
$db = new PDO("sqlite:/Users/dmakovec/test.sqlite");
//$db = new PDO("mysql:host=localhost;dbname=test","root","");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
foreach($db->query("SELECT * from test") as $row) {
print "{$row['id']} : {$row['name']}\n";
}
} catch (PDOException $e) {
print <<<EOF
An Exception occurred:
File: {$e->getFile()}
Line: {$e->getLine()}
Code: {$e->getCode()}
Message: {$e->getMessage()}
Trace:
{$e->getTraceAsString()}
EOF;
}
|