PHP provides the adept developer with the
tools to create some powerful Web solutions. Moreover, with the
ability to add additional functionality through extensions, it
makes creating solutions easy.

The PHP Extension and Application Repository
(PEAR) is a framework that helps
developers create solutions through high-level APIs or packages.
The PEAR is also a distribution system in that it provides a means
for automatic installation of these packages. This week, I’ll
highlight what I believe is one of the more important packages: the
DB package.

The DB package is an object-oriented style
abstraction layer to PHP’s underlying database connectivity and
querying; it’s compatible with both PHP 4 and 5. The DB package
currently supports the following extensions: dbase, fbsql,
interbase, informix, msql, mssql, mysql, mysqli, oci8, odbc, pgsql,
sqlite, and Sybase.

If you’ve used the mysql_* functions to attach
to and query MySQL databases, you’re probably familiar with the
different resultsets available. The most common two resultsets come
as either an array with each field being an indexed element or as
an associative array with each field being a key/value pair. PEAR
DB provides these two resultsets, as well as an object type, which
creates a dynamic object where each field is a property of the
object. The associative array and the object type are probably
going to be the most familiar to developers coming from ADO
connectivity on Windows machines.

In ADO, a resultset (or a Recordset) is
returned as a navigable collection of rows. Each row is a
collection of Field objects. Each Field object has a Name and a
Value property (i.e., the name of the field and the data contained
within that field, respectively). Here’s an example of how to
access field data in PHP:

require_once(“DB.php”);
$dsn = “mysql://username:password@localhost/database”;
$options = array(
    ‘debug’
      => 2,
    ‘portability’ =>
DB_PORTABILITY_ALL,
);
$db =& DB::connect($dsn, $options);
$db->setFetchMode(DB_FETCHMODE_ASSOC);
$res =& $db->query(“SOME SQL STRING”);
$row =& $res->fetchRow();
echo $row[‘field1’];

And here’s how to do it using ADO in
VBScript:

Dim conn, rs
Set conn = Server.CreateObject(“ADODB.Connection”)
Set rs = Server.CreateObject(“ADODB.Recordset”)
conn.ConnectionString = “some connection string”
Set rs.ActiveConnection = conn
rs.Open “SOME SQL STRING”
Response.Write rs.Fields(“field1”).Value

You can see the similarities between the two
routines. First, you establish a connection with the database.
Then, you send a query and receive a resultset. Just like ADO, the
PEAR DB package provides an abstraction to all the lower-level
mechanics involved with communicating with the database server.

If you’re new to PEAR, then you’ll want to know
how to implement the packages on your own system. As a distribution
system, PEAR makes packages easy to implement. I don’t have much
control over my hosted Web site, so I’m restricted to my own real
estate on the virtual host via FTP and a host provider-supplied
“control panel.”

Before you install PEAR, copy the contents of
this script to a PHP file on your host system. I created a
directory under the default Web directory on my host called
install, and copied the script to a file called go-pear.php. I
changed the directory permissions to allow access for the installer
script. Next, I navigated my browser to the go-pear.php installer page,
and the script took care of the rest. Once it was done, I followed
the link to the Web package installer, changed the configuration
options (which I located by following the configuration link), and
started installing packages with no problem.

Note: I ran the installer
and my PHP scripts on a hosted site supporting PHP 4.3.8 and MySQL
4.0.20-standard. The server OS is Linux 2.4.21-15.ELsmp, and the
Web server is Apache 1.3.31.

Keep your developer skills sharp by automatically signing up for TechRepublic’s free Web Development Zone newsletter, delivered each Tuesday.