Re: [PHP-PEAR] PHPLib and Pear merge

[email protected] ("Paul Meagher") Wed, 7 Mar 2001 09:31:58 -0400
Newsgroups php.pear
Message-ID <008c01c0a70a$fc689dc0$6c35de18@datavore>
Upon hearing about the benchmarks I questioned whether I had made the right
decision to use the PEAR DB abstraction layer.

I decided to create a second parallel version of the code I was developing
so that it used PHPLIB's db_sql class for mysql.

What I discovered is that the port wasn't that difficult to do.

** First example:

The PHPLIB way:

  function deleteCat($cat_id)
  {
    global $db;
    $sql = "DELETE FROM $this->cat_table WHERE $this->cat_id='$cat_id'";
   if ($db->query($sql)) {
     return true;
   } else {
      return false;
   }
  }

The PEAR: DB way:

  function deleteCat($cat_id)
  {
    global $db;
    $sql = "DELETE FROM $this->cat_table WHERE $this->cat_id='$cat_id'";
    $sth = $db->query($sql);
    if (DB::isError($sth)) {
      // print $sth->getMessage();
      return false;
    } else {
     return true;
    }
  }

** Second example

PHPLIB way of dumping records

     $db->query($sql);
     while($db->next_record()) {
       $cat_id = $db->f('cat_id');
       $cat_pos = $db->f('cat_pos');
       $cat_name = $db->f('cat_name');

PEAR way:

    $sth = $dbh->query($sql);
    while($row = $sth->fetchRow()) {
       $cat_id = $row['cat_id'];
       $cat_pos = $row['cat_pos'];
       $cat_name = $row['cat_name'];

Ok, so I suppose they are a bit diff but I thought I would leave you with
some grist to consider whether it would  make sense to make the API's
between PEAR:DB and DB_Sql sufficiently similiar that one would switch
abstraction layers with a simple a local.inc file (or whatever you want to
name your local subclassing file).

Regards,
Paul Meagher