PHP Data Objects

PDO by Wes Furlong
Ships with PHP 5.1

-The PDO Solution:
. Move PHP specific stuff into one extension
. Database specific stuff (only) in their own extensions
. Data access abstraction, not database abstraction

-Features:
. Performance:
. Native C code beats a scripted solution
. Power:
. Gives you common DB features as a base
. Still be able to access specialist functions

-What can it do?
. Prepare/execute, bound parameters
. Transactions
. LOBS

-Available Drivers
. Mysql, PostgreSQL,
. ODBC, DB2, OCI,
. Sqlite, …

– Persistent PDO
. Connection stays alive between requests
. Can specify your own cache key


$dbh = new PDO ($dsn);
$stmt = $dbh->prepare("SELECT * FROM FOO");
$stmt->execute();
while ($row = $stmt->fetch()) {
print_r($row);
}

– Forward-only cursors
. Also known as “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()

– Data typing
. Very loose
. Uses strings for data
. Gives you more control over data conversion
. Supports integers where 1:1 mapping exists
. Is float agnostic (PDO is precise)

– Smarter queries
. Quoting is annoying, but essential

– Resources:
docs.php.net/pdo
netevil.org