AskBee.NET
Google
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: Using common sql commands
 

Retrieve information with get and find

In this chapter we will use the configuration files and database from previous chapter.
To get the records for one specific client we will use the get() method.
//include configuration file
require_once( 'startup.php' );

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

//get the records for the clients_id 10, where clients_id is primary key.
$client->get(10);
//display client details
echo $client->name . ' - ' . $client->email . ' - ' . $client->job;

//another usefull way to display client details is to create an array with client details, using toArray() method
$arr_client = $client->toArray();
print_r( $arr_client );
 
In the next example we will retrieve data based on a (key,value) pair.
//include configuration file
require_once( 'startup.php' );

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

//get the data based on a key,value pair
$client->get('email','Test@askbee.net');
 

Fetch multiple records

We will search for all the clients that have the 'web developer' job
//include configuration file
require_once( 'startup.php' );

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

//get the clients
$client->job = 'web developer';
//find() method returns the number of records
$nr_clients = $client->find();

//check if clients were found
if( $nr_clients ) > 0 )
{
    //display found clients
    while( $client->fetch() )
    {
       echo $client->name;
    }
}
else
{
    No client found.
}

Insert a new record

//include configuration file
require_once( 'startup.php' );

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

//set clients properties
$client->name = 'Another Test';
$client->email = 'another@whateverdomainabc.com';
$client->job = 'general manager';
//insert the new record into database and returns the ID
$new_client_id = $client->insert();

Update an existing record

//include configuration file
require_once( 'startup.php' );

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

//get client properties with ID=10
$client->get( 10 );

//set another job for the client
$client->job = 'Project manager';

//update client
$client->update();

Delete a record from database

//include configuration file
require_once( 'startup.php' );

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

//get client properties with ID=10
$client->get( 10 );

//delete this client
$client->delete();

//if you want to delete all the clients that have the 'web developer' job, you can use WhereAdd() method
$client->whereAdd( "job='web_developer'" );
$client->delete( DB_DATAOBJECT_WHEREADD_ONLY );

//Notice that in this case you must use DB_DATAOBJECT_WHEREADD_ONLY constant.
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.
prev page Previous Page
Next Page next page
Advertising
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
www.codeworkshq.com
www.averagedesign.com
www.4invent.com
Catering
Anunturi online
revelion 2009
Privacy Policy | Top Searches | Cheap web hosting | Ringtomes | Shared Hosting Valid HTML 4.01 Transitional Valid CSS!