Building a Database Agnostic PHP Application With ADOdb

This article explains what ADOdb is and provides examples on how it can be used with PHP to build an application that connects to any supported database server.

What is ADOdb

ADOdb is and open source database abstraction library for PHP. The purpose of a database abstraction library is to create uniformity in communicating with multiple database servers. Think of it this way, for your application to support multiple databases it would need different lines of code to perform the same functions on each.  With an abstraction library the application only needs one set of code to communicate with any database the library supports. This drastically increases the number of database server options available for your PHP application. 

Active Record

ADOdb also supports Active Record.  Active Record uses Object Relation Mapping (ORM) to communicate with your database through PHP data objects by mapping them to tables and rows. This method removes the need to write SQL queries speeding up application development.

Hosting Platform

ADOdb is a PHP library that can be used on any Hosting.com platform service with PHP and all pre-installed database servers.

Installation

Installing ADOdb can be completed in three steps:  

  1. Obtain the latest version of ADOdb.
  2. Copy the ADOdb folder to a directory within your application.
  3. Include the adodb.inc.php in your application's configuration file.

Usage Examples

Connection Example:

 
<?php

include('adodb/adodb.inc.php');
$db = ADONewConnection('mysql'); # eg 'mysql' or 'postgres'
$db->debug = true;
$db->Connect($server, $user, $password, $database);

?> 

Sample Query:


<?php

$rs = $db->Execute('select fname,lname from table');

print "<pre>";
print_r($rs->GetRows());
print "</pre>";  

?>

Active Record Usage:



<?php

include('adodb/adodb.inc.php');
include('adodb/adodb-active-record.inc.php');

$db = ADONewConnection('mysql'); # eg 'mysql' or 'postgres'
$db->debug = true;
$db->Connect($server, $user, $password, $database);
ADOdb_Active_Record::SetDatabaseAdapter($db);
ADODB_Active_Record::$_quoteNames = true;

#the following example use a table that was created with this statement
$db->Execute("CREATE TABLE `users` (
                `id` int(10) unsigned NOT NULL auto_increment,
                `fname` varchar(100) NOT NULL default '',
                `lname` varchar(100) NOT NULL default '',
                PRIMARY KEY  (`id`)
            ) ENGINE=MyISAM;
           ");

#create an empty class to work with our new table 
class users extends ADOdb_Active_Record {
  var $_table = 'users';
}
$users = new users();

#insert a new record
$users->fname = 'Ronald';
$users->lname = 'Reagan';
$users->save();

# $users->id is now equal to the last inserted id. For this example we will assume the id is 1.

#update a record
$users->id = 1;
$users->fname = 'Nancy';
$users->save();

#select a single record
$users->load('id=1');

#now the users object contains the full row of data where id = 1.   

?>