PDO: PHP Data Objects
Wez Furlong (Message Systems, Inc.)
2:45pm Tuesday, 09/16/2008
The Basics Room 209
PHP 5.1 features a new data access layer, PHP Data Objects (PDO), that provides a clear, simple (but powerful), unified API for working with all our favorite databases. Features include prepared statements with bound parameters (for all databases, even those that don’t natively support them), transactions, cursors, LOBs, and flexible error handling. Also examined are the issues of cross-database portability and how best to use PDO to avoid some of the pitfalls associated with that topic. Join Furlong to learn more about the future of database access in PHP.
Photo of Wez Furlong
Wez Furlong
Message Systems, Inc.
Wez Furlong is Director of Engineering at Message Systems where he works on the fastest MTA on Earth.
Wez is a PHP Core developer and active OpenSource contributor. He currently resides in Sykesville, Maryland with his wife, son and loyal pup.
Website – http://netevil.org/
PDO::ERRMODE_SILENT
PDO::ERRMODE_WARNING
PDO::ERRMODE_EXCEPTION
forward-only cursors
-a.k.a. “unbuffered” queries in mysql parlance
-they are the default cursor type
-rowCount() doesn’t have meaning
-FAST
-other queries are likely to block
-you must fetch all remaining data before launching another query
-$stmt->closeCursor();
Fetch modes
-PDO::FETCH_COLUMN
Iterators
$dbh = new PDO($dsn);
$stmt = $dbh->query(
“SELECT name FROM FOO”,
PDO::FETCH_COLUMN, 0);
foreach ($stmt
Changing Data
$deleted = $dbh->exec(
“DELETE FROM FOO WHERE 1”);
$changes = $dbh ->exec(
“UPDATE FOO SET active=1”
“WHERE NAME LIKE ‘%joe%’”);
Autonumber / sequences
$dbh->exec(
“insert into foo values(…)”);
echo $dbh->lastInsertId();
Prepared Statements
$stmt = $dbh -> prepare(
“INSERT INTO CREDITS (extension, name)”
“VALUES(:extension, :name)”);
$stmt -> execute(array(
‘extension’ => ‘xdebug’
‘name’ => ‘Derick Rethans’
));




