Notice: Use of undefined constant HTTP_SERVER - assumed 'HTTP_SERVER' in /srv/www/ on line 3
AskBee.NET | php | DB DataObject | introduction
AskBee - Articles, Tutorials.
   Categories    Resources
   Photo Gallery    Contact

Categories / php / DB_DataObject

By Bee
Last updated: September 14, 2005
  1. Configuration - A simple example
  2. Using common sql commands - select, insert, update, delete
  3. Joins - Linking related tables
  4. SQL Casting
  5. Debug queries
  6. Other usefull features
Source code: A Simple Example.


I recently discovered DB_DataObject database abstraction layer class from PEAR, it's a database class that every web developer needs.
In this tutorial I will show you how to take advantage of the DB_DataObject class and you will notice how this class will strongly affect your developing time.
important note Please be aware that using DB_DataObject class does not save you from learning sql, therefore you should already know how to build a query.
As you will see later, in all the articles posted on AskBee.NET, you will find full working and tested examples. So let's get it started.
How does DB_DataObject package works? It will create classes for every table in the database.
For example if we have a table called clients in our database, a file called Clients.php will be created. This file will contain a class called DataObjects_Clients that extends the base class DB_DataObject.
important note WARNING! Zend Optimizer: due to a bug in the optimizer, you will have to either reduce the optimization level, or define the constant DB_DATAOBJECT_NO_OVERLOAD = 0 otherwise PHP may segfault

Step One - PEAR package installation

First of all you need to download and install PEAR packages from PEAR.
The packages we need are:
  • PEAR - PEAR Base System 1.3.6
  • DB - DataBase Abstraction Layer 1.7.6
  • DataObject - An SQL Builder, Object Interface to Database Tables 1.7.15
as DB_DataObject package is dependent on other two. You should download the latest stable version of every package.
You should put all the PEAR packages in one directory, and its structure should be the same like the one in the next file PEAR directory structure.

Step Two - Create Directories Structure

I will use includes directory and in this directory pear directory will be created and another directory called dbdataobject. In the last directory dbdataobject classes are generated. If you are working in a unix environment make sure full permissions are set for this directory.

Step Three - Create Database Structure

We will create just one table first with four fields. The table name is clients and it has the strucure below.
Simple Database Design

Step Four - Dynamic Class Generation

So as I said before, we need to generate dynamic classes for every table in our database. Therefore we need to create two files: createTables.php and dbconfig.php
First file createTables.php is the script that will create this tables.
//define absolute server path, for windows
define('ABS_SERVER', 'D:/PHP/dbdataobject_article/'); //absoulte server path

//set include path for windows
ini_set('include_path', ABS_SERVER . ';' . ABS_SERVER . 'includes/pear/;' . ABS_SERVER . 'includes/');

//set include path for linux
//ini_set('include_path', '.:./includes/pear:./includes');

$_SERVER['argv'][1] = "dbconfig.php";
Second file dbconfig.php contains database information and location paths

database = mysql://username:password@hostname/database_name
schema_location = includes/dataobject
class_location = includes/dataobject
require_prefix = DataObjects/
class_prefix = DataObjects_
Make sure you remove this two files after the website goes live!!!

After you've created this two files, type in your browser http://your_website_link/createTables.php and then go to the class_location directory (includes/dataobject in this case) and you will find the classes created.
Host your PHP-MySQL site now

Step Five - Simple Example

A simple example of getting records from clients table.
We create a file named startup.php where we can find the database settings. This file should look like this.
//define absolute server path, for windows
define('ABS_SERVER', 'D:/PHP/dbdataobject_article/'); //absoulte server path

//set include path for windows
ini_set('include_path', ABS_SERVER . ';' . ABS_SERVER . 'includes/pear/;' . ABS_SERVER . 'includes/');

//set include path for linux
//ini_set('include_path', '.:./includes/pear:./includes');

//include dataobject classes
include_once('DB/DataObject.php' );
//define database configuration values
$options = &PEAR::getStaticProperty('DB_DataObject','options');

$options = array(
'database' => 'mysql://username:password@hostname/database_name',
'schema_location' => ABS_SERVER . 'includes/dataobject//',
'class_location' => ABS_SERVER . 'includes/dataobject/',
'require_prefix' => 'DataObjects/',
'class_prefix' => 'DataObjects_',
And the second file is the file that gets all the records from the database.
//include configuration file
require_once( 'startup.php' );

//create client object
$client = DB_DataObject::factory( 'clients' );

//get all the clients
//display clients details
while( $client->fetch() )
    echo $client->name . ' - ' . $client->email . ' - ' . $client->job;
This example was tested on a Windows platform, PHP 4.3.9, MySQL 4.0.12, Apache 1.3.33
Download the whole working archive Simple Example.
Next Page next page
Are you paying more than $170 per month on your credit cards?
Let us help you. Free Debt Relief !
Host your website now
Unlimited Hosting
Cheap Hosting
Bee recommends:
SEO Articles
AskBee Hosting Plans
Affordable hosting
AskBEE Directory
Anunturi online
revelion 2012
anvelope iarna si all season
Cumpara Bitcoin Romania
Taxi Heathrow to London
Privacy Policy | Top Searches | Cheap web hosting | Ringtomes | Shared Hosting Valid HTML 4.01 Transitional Valid CSS!