#native_company# #native_desc#
#native_cta#

PEAR Primer Page 4

By Joe Stump
on December 26, 2002

The PEAR DB Class

I could write two or three articles on the DB class included in PEAR, which is why I’ll only briefly cover its
use here. I use it flawlessly with MySQL and thus far my only complaint is that it uses sequence tables instead
of merely returning the insert id in MySQL’s auto_increment fields.

<?php

  require_once('Base.php');

  $db =& DB::connect(BASE_DEFAULT_DSN,true);

  if(!
DB::isError($db))

  {

    
$db->setFetchMode(DB_FETCHMODE_ASSOC);

    

    
$sql "SELECT *

            FROM table

            WHERE foo='bar'"
;

    $result $db->query($sql);

    if(!
DB::isError($result) && $result->numRows())

    {

      while(
$row $result->fetchRow())

      {

        echo 
'&lt;li&gt;'.$row['foo']."n";

      }

    }

  }

  else

  {

    echo 
$db->getMessage();

  }

?>



This is the most basic of examples on how to use the DB class, but those of you who have used Perl’s DBI or the many
PHP database functions will note the structure of connecting to a database is pretty much intact. One thing to
note is that the result is a separate class from the DB class with its own functions, etc. For a more detailed
overview of the DB class check out
http://pear.php.net/manual/en/core.db.php
.

1
|
2
|
3
|
4
|
5
|
6
|
7