HomePHP Page 9 - PHP Application Development With ADODB (part 1)
Hitting The Limit - 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.
The SelectLimit() method can be used to restrict the number of rows retrieved.
<?php
// uncomment this to see plaintext output in your browser
// header("Content-Type: text/plain");
// include the ADODB library
include("adodb.inc.php");
// 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
connect!");
// execute query
// get 5 rows, starting from row 3
$query = "SELECT * FROM library";
$result = $db->SelectLimit($query, 5, 3) or die("Error in query: $query.
" . $db->ErrorMsg());
// iterate through resultset
while (!$result->EOF)
{
echo $result->fields[1] . " - " . $result->fields[2] . "\n";
$result->MoveNext();
}
// clean up
$db->Close();
?>
In this case, the SelectLimit() method can be used to obtain a subset of
the complete resultset retrieved from the database. The first argument to the method is the query to execute, the second is the number of rows required, and the third is the row offset from which to begin.
Finally, you can obtain a list of databases on the server via the MetaDatabases() method, and a list of tables within the current database via the MetaTables() method.
<?php
// uncomment this to see plaintext output in your browser
// header("Content-Type: text/plain");
// include the ADODB library
include("adodb.inc.php");
// 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
connect!");
// get database list
echo "Databases:\n";
foreach($db->MetaDatabases() as $d)
{
echo "* $d\n";
}
// get table list
echo "\nTables in current database:\n";
foreach($db->MetaTables() as $table)
{
echo "* $table\n";
}
// clean up
$db->Close();
?>