ADODB and PHP

I l @ ve RuBoard

Now that we have looked at ADO and PHP, we will look at a database library for PHP called ADODB. ADODB was developed in PHP by John Lim. As he states in his ADODB Manual v1.12, "PHP's database access functions are not standardised . This creates a need for a database class library to hide the differences between the different databases (encapsulate the differences) so we can easily switch databases." Thus, ADODB provides an ADO-like syntax for connecting to the various PHP-supported databases.

When you use ADODB, remember that ADODB is not ADO. ADODB is a PHP library that wraps around PHP-supported databases, whereas ADO is a set of COM objects that wrap around OLE DB. Thus, it is for use with any database that supports OLE DB.

Installing ADODB

You can download ADODB from http://php.weblogs.com/ADODB. You can download a zip file for Windows or a tgz file for UNIX systems. All you need to do then is uncompress the file's contents into a directory that your code can access.

Using ADODB

To use the ADODB library, you use the library's functions and objects to query your database in same way as the ADO examples earlier in this chapter. Listing 10.12 shows you how to do this.

Listing 10.12 Querying the Database
 <?php  include('Files\adodb.inc.php');  $db = &ADONewConnection('ado');   $myDSN="SERVER=emma;DATABASE=phpbook;";  $db->Connect($myDSN, "sa", "emma", 'SQLOLEDB');       $objrs = &$db->Execute('select * from Names');       while(!$objrs->EOF) {           print $objrs->fields[1] . " " . $objrs->fields[2] . "<BR>";            $objrs->MoveNext();       }  $db->close();  ?> 

At first glance, you can see that this code is indeed similar to the ADO code we developed earlier in this chapter. However, because the ADODB library wraps around the standard PHP-supported database types, you are subject to the same rules concerning database support on various platforms (for example, ADO works only on Windows platforms, but ADODB works on all the supported PHP platforms) and the same rules concerning database library requirements (such as the Oracle client for connecting to Oracle databases). Let's look at how the query code works.

You use ADODB simply by making use of the ADODB.inc.php file. This file provides you with the necessary functions and objects of the ADODB library. You therefore include this file in your PHP code every time you use ADODB.

 include('Files\adodb.inc.php'); 

Here the ADODB library is installed in a directory called Files, which is directly below the directory the code is in. After you include the ADODB.inc.php file, you need to define what kind of database type you will query:

 $db = &ADONewConnection('ado'); 

Here you create an ADO database type. Thus, your database connection and query are run through ADO. Next, you need to create the connection to the database:

 $myDSN="SERVER=emma;DATABASE=phpbook;";  $db->Connect($myDSN, "sa", "emma", 'SQLOLEDB'); 

Here you set up a new DSN connection within the Connection method. Next you run a SQL statement against the database using your connection:

 $objrs = &$db->Execute('select * from Names'); 

Just like with ADO, calling the Execute method returns a Recordset object, and that lets you run through the records in the database table:

 while(!$objrs->EOF) {      print $objrs->fields[1] . " " . $objrs->fields[2] . "<BR>";       $objrs->MoveNext();  } 

As soon as the query is complete, you close the connection:

 $db->close(); 

If you run the script, you can see that the database data is displayed, as shown in Figure 10.8.

Figure 10.8. Querying the database using the ADODB library and PHP.

graphics/10fig08.gif

Using the ADODB Library or ADO

The ADODB library not only provides you with ADO-like syntax for querying, updating, adding to, and deleting from PHP-supported databases, but it also implements a session back end for PHP 4 sessions, includes database debugging facilities, and handles database transactions.

However, the ADODB Library does not provide all the functionality of using ADO directly in code. For example, it has no wrapper code for creating recordsets unless they are created with a Connection object, and it has no support for ADO Parameter objects.

I l @ ve RuBoard


PHP Programming for Windows
PHP Programming for Windows (Landmark (New Riders))
ISBN: 0735711690
EAN: 2147483647
Year: 2002
Pages: 99

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net