On CodePoets.co.uk today, a new tutorial introduces you to the PEAR DB package, giving you a howto guide on performing simple queries with its functionality.
PEAR::DB, provides a uniform, cross platform, cross database method for connecting to databases, when writing PHP applications/scripts. Extensive documentation can be found online here This article aims to show briefly, how queries and updates can be performed when using PEAR DB.
They list a few reasons why one might want to use the PEAR DB package over the normal PHP database functions before they get into the examples. There are four examples - making the connection, querying the database, what to do to avoid SQL injections, and updating your database with prepared statements.










# Query once, so mysql query cache is not an issue
$DB->query("SELECT * FROM medewerker");
# ----------
$output1 = "";
$time = microtime(true);
$result_pear = $DB->query("SELECT * FROM medewerker");
while ($row = $result_pear->fetchRow(DB_FETCHMODE_ORDERED)) $output1 .= join(";", $row) . "n";
echo "Pear result - " . $result_pear->numRows() . " rows: " . number_format((microtime(true) - $time) * 1000, 2, '.', '') . "n";
# ----------
$output2 = "";
$time = microtime(true);
$result_values = $DB->getAll("SELECT * FROM medewerker");
foreach ($result_values as $row) $output2 .= join(";", $row) . "n";
echo "Pear values - " . sizeof($result_values) . " rows: " . number_format((microtime(true) - $time) * 1000, 2, '.', '') . "n";
# ----------
$output3 = "";
$time = microtime(true);
$result_mysql = mysql_query("SELECT * FROM medewerker");
while ($row = mysql_fetch_row($result_mysql)) $output3 .= join(";", $row) . "n";
echo "Mysql result - " . mysql_num_rows($result_mysql) . " rows: " . number_format((microtime(true) - $time) * 1000, 2, '.', '') . "n";
Result:
Pear result - 2427 rows: 3522.35
Pear values - 2427 rows: 3881.87
Mysql result - 2427 rows: 194.67
Do I need to say more.
I'm planning on comparing all common DB interfaces. So keep your eye's out for an article on www.phpit.net.
Good luck,
Arnold Daniels
www.helderhosting.nl