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:
- A structured library of open-source code for PHP users
- A system for code distribution and package maintenance
- A standard style for code written in PHP
- The PHP Foundation Classes (PFC)
- The PHP Extension Code Library (PECL)
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):
'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
\n";
echo "name => $data->name
\n";
}
?>
That is a very simple example to show how the code could look
with an abstraction library like PEAR::DB.