HomePHP Page 6 - PHP Application Development With ADODB (part 1)
Getting It All - PHP
PHP comes with a different API for different database types -whcih usually means a code rewrite every time your databaseadministrator decides to experiment with something new. But fear not -help is at hand, in the unlikely form of ADODB, a powerful databaseabstraction library for PHP applications.
You can replace the Execute() method with the GetAll() method, which returns the complete resultset as a two-dimensional array of field-value pairs. This array can then be processed with a simple "foreach" or "for" loop. Consider the following example, which demonstrates:
// uncomment this to see plaintext output in your browser
// header("Content-Type: text/plain");
// include the ADODB library
// create an object instance
// configure library for a MySQL connection
$db = NewADOConnection("mysql");
// open connection to database
$db->Connect("localhost", "john", "doe", "db278") or die("Unable to
// execute query
$query = "SELECT * FROM library";
$result = $db->GetAll($query) or die("Error in query: $query. " .
// clean up
// uncomment the following line to see the returned array.
// iterate through resultset
// print column data in format TITLE - AUTHOR
foreach ($result as $row)
echo $row . " - " . $row . "\n";
// get and print number of rows in resultset
echo "\n[" . sizeof($result) . " rows returned]\n";
In this case, the GetAll() method creates a two-dimensional array of
result data, which looks something like this.
 => Array
 => 14
[id] => 14
 => Mystic River
[title] => Mystic River
 => Dennis Lehane
[author] => Dennis Lehane
 => Array
 => 15
[id] => 15
 => For Kicks
[title] => For Kicks
 => Dick Francis
[author] => Dick Francis
 => Array
 => 16
[id] => 16
 => XML and PHP
[title] => XML and PHP
 => Vikram Vaswani
[author] => Vikram Vaswani
... and so on ...
This array can then be iterated over by a "foreach" loop, and the required
values accessed as regular array elements. The size of the array is equivalent to the total number of rows in the resultset.
This method provides a useful alternative to the Execute() method, especially in situations when you would prefer to have the entire recordset available at once, rather than one row at a time.