Skip navigation.

PDO

PDO is a new database API that will be part of the official PHP 5.1 release. AFAIK, PDO will not be a full-fledged database abstraction library, but will provide a standard database API for PHP.

Here is a sample taken from the PDO download:

<?php
   $x = new PDO("odbc:ram", 'php', 'php', array(PDO_ATTR_AUTOCOMMIT => 0));
   $stmt = $x->prepare("select NAME, VALUE from test where value like ?");
   $the_name = 'bar%';
   $stmt->execute(array($the_name)) or die("failed to execute!");
   $stmt->bindColumn('VALUE', $value);
   while ($row = $stmt->fetch()) {
   	echo "name=$row[NAME] value=$row[VALUE]\n";
   	echo "value is $value\n";
   	echo "\n";
   }
    echo "Let's try an update\n"
   $stmt = $x->prepare("INSERT INTO test (NAME, VALUE) VALUES (:name, :value)");
$stmt->bindParam(":name", $the_name, PDO_PARAM_STR, 32);
   $stmt->bindParam(":value", $the_value, PDO_PARAM_STR, 32);
for ($i = 0; $i < 4; $i++) {
   	$the_name = "foo" . rand();
   	$the_value = "bar" . rand();
	if (!$stmt->execute()) {
   		break;
   	}
   }
echo "All done\n";
?>
Highlights of PDO include the unified object-oriented API, compiled statements are now first class objects (the PDOStatement class), and better support for bind variables, which will give a substantial speed boost for performance freaks.

Does this make ADOdb superflous? If you are looking for something very simple, then ADOdb is probably not for you. However ADOdb still has a big role to play, because PDO does not provide an infrastructure for enterprise database access, such as portable handling of data types and schemas, recordset caching, sql logging and tuning, session management and other nice ADOdb features. Secondly, if you come from a Microsoft background (like me), it is very easy to learn ADOdb.

So the next question is, how to extend ADOdb to support PDO? I think we can retain the existing ADOdb infrastructure, treating PDO as just another ADOdb driver. At the same time, we will add PDO specific extensions to the ADOdb PDO driver. So the classic ADOdb calling conventions will still work:

        include('adodb.inc.php');
	$DB = NewADOConnection('pdo');
	$DB->Connect($host, $user, $pwd, $db);
	$rs = $DB->Execute("select * from table where name=?",array('Jill'));
	while (!$rs->EOF) {
		var_dump($rs->fields);
		$rs->MoveNext();
	}
And we will add a new ADOdb statement class to support PDO conventions:
        include('adodb.inc.php');
	$DB = NewADOConnection('pdo');
	$DB->Connect($pdo_connection_string);
	$stmt = $DB->PrepareStmt("select * from table where name=?");
	$rs = $stmt->Execute(array('Jill'));
	foreach ($rs as $row) {
		var_dump($row);
	}

Wez has more PDO examples. A PDO discussion at sitepoint.