Contents / Previous / Next


PEAR and DBs

pear.php.net and PEAR and DBs documentation


What is PEAR?

PEAR is short for "PHP Extension and Application Repository" and is pronounced just like the fruit. The purpose of PEAR is to provide: PEAR is still pretty much a work in progress.


PEAR DB Package

PEAR::DB is the most popular PEAR package.
It allows the developer to write code that could be used for several different database servers: MySQL, PostgreSQL, and Oracle, etc. .

Example (insert an entry on a table):

<?php // Include the appropriate PEAR classes require_once("DB.php"); $dsn = array( 'phptype' => 'mysql', 'hostspec' => 'localhost', 'database' => 'test_db', 'username' => 'test_user', 'password' => 'test_password' ); $dbh = DB::connect($dsn); $stmt = "SELECT id, name FROM examples ORDER BY id"; $result = $dbh->simpleQuery($stmt, DB_FETCHMODE_ASSOC); if ($dbh->numRows($result) > 0) { $data = (object) $dbh->fetchRow($result, DB_FETCHMODE_ASSOC); echo "id => $data->id<br>\n"; echo "name => $data->name<br>\n"; } ?> That is a very simple example to show how the code could look with an abstraction library like PEAR::DB.