class.DBI
DBI/DBD Like Interface for PHP

Main Page

Linux
News Links
Web
PHP
Game Links
Search
 Google

Food
 Epicurious
 S.O.A.R.

Music
 BMG
 Play

TV/Movies

PDX
Real Estate
Funny
Health
Travel
Stores
Lighting
PIC
Misc.

About This Site

 

 

Notice: With the advent of Pear::DB this class is obsolite and will no longer be maintained.

Quick Links:


I never want to code myself into a corner and that is one of the reasons that I love DBI/DBD in perl. I am also very fond of PHP but have always mourned the lack of a DBI like interface. So I wrote one.

At the moment there is almost no documentation however if you have used the perl DBI/DBD then most of this should make sense to you (see the notes below for differences). I do recommend reading the said perl documentation -- man DBI -- and looking at the sample code below.


Supported Databases

  • Informix
  • MySQL
  • PostgreSQL ( by Joseph Artsimovich)
  • Sybase (by Roland Lichti)
  • Oracle 8 (by Philippe Allart)
  • Interbase (by Dobrica Pavlinusic)


News
05-May-2001 Version 0.5.0
  • Change class.DBD::Pg so that it uses UNIX domain sockets instead of network sockets when connecting to the localhost. (James D. McIninch).
  • Change all but the sybase STH create code to use the reference to the parent DBH object rather than copying it. (Mads Lie Jensen)
  • BIG BUG FIX: Fixed returning '0E0' when there was an error in dbh_do. (Zack Erhart)
31-Jan-2001 Version 0.3.8
  • Fixed parse_dbi_string regex (Dan Morgan).
  • Allow prepared statements in the mysql and Pg DBD drivers (Norman Clarke).
12-Sep-2000 Version 0.3.7
Added fixed class.DBD::sybase (by Roland Lichti) that adds the missing insert_id function.
30-Aug-2000 Version 0.3.6
  • Interbase module by Dobrica Pavlinusic.
  • Oracle 8i module by Philippe Allart.

Really, This will become version 1.0.0 unless I get some other show stopper bugs or major feature requests.

07-May-2000 Version 0.3.5
  • Added rows( ) function to all of the STH objects. This returns the number of rows that will be fetched by the query.

This will become version 1.0.0 unless I get some other show stopper bugs or major feature requests.

30-Mar-2000 Version 0.3.4
  • New Pg DBD driver by Joseph Artsimovich.
    joseph_a<at>mail<dot>ru
30-Mar-2000 Version 0.3.3
  • Added a warning message to class.DBI if you try to connect to two different databases using the same host, user, and password. This is an issue because PHP reuses database connections so one ends up just having two DBI objects that point to the same database.
  • Added comments on the web page that you have to use $sth->errstr when checking for problems with prepared statements.
28-Mar-2000 Version 0.3.2.
Fixed the DBD::* class files to not ignore the is_persistent flag. Fixed prepare( ) in DBI so that you can have more than DBI object connected to more than one database.
18-Mar-2000 Version 0.3.1 Full announcement:
More modular 'DBD' drivers: each database driver is in an individual file that gets dynamically loaded (thanks to Roland Lichti for feedback and help). Support for MySQL, Sybase (Roland Lichti), and Informix. Updated sample code and function documentation on the web page. Consider this a beta release. If you have access to other PHP supported databases, please contribute. However, I ask that you let me know ahead of time so I can coordinate and prevent duplicated efforts.

Changes
Download

17-Mar-2000 Version 0.3.0!
Changes
Download
16-Mar-2000 Initial code release version 0.1.0.


Notes:

  • The ->do( ) is called ->dbh_do( ) as PHP sees 'do' as a reserved word even though it is a class function definition.
  • Not every function in the perl DBI/DBH is supported.
  • To get errors on prepared statements, you have to check $sth->errstr and NOT $dbh->errstr (see the code below).
  • You can not make twp DBI objects that connect to two different databases if they share the driver, host, user, and password (at least for MySQL). This is because PHP reuses database connections if it sees all of the above match a previous connection. What ends up happening is the last DBI object becomes the selected database for all DBI objects that share the said parameters. To get around this, you have to change something in the connection, e.g. the user name. class.DBI will warn you if you try to do this.
  • To install this, put the class.DBI and class.DBD::* files somewhere in your php include path.


Example Code and Some Embedded Documentation

require( 'class.DBI' );
//Get a new dbh. Note the way that we check to see 
//  if the connection was made or not.  This is 
//  because as far as I can tell, the "New" 
//  constructor cannot return anything but '$this', 
//  e.g. 'undef'.  Change the 'dbi:...' to 'pdbi:...' for 
//  persistent connections.
$dbh = new DBI( 'dbi:mysql:database;mysql_host', 'user', 'secret' );
if( ! $dbh->dbh ){
  echo "Could not connect to the database.<br>\n";
  exit( );
}

//If you want to use Informix, be sure to setenv the two variables.
setenv( "INFORMIXDIR=/path/to/informix" );
setenv( "INFORMIXSERVER=defaultserver" );
$ifx_dbh = new DBI( 'dbi:Informix:database@hostalias', 'user', 'secret' );


$sth = $dbh->prepare( 'SELECT COUNT(*) FROM some_table' );
if( $sth ){ 
  $sth->execute( ); 
  echo "sth executed.<br>\n";
  $row = $sth->fetchrow_array( );
  echo "Row: ".$row[0]."<br>\n";
  $sth->finish( );
} else {
  echo "Could not prepare query.<br>\n";
}


//Get data by column name.

$sth = $dbh->prepare( 'SELECT * FROM some_table' );
if( $sth ){ 
  $sth->execute( ); 
  while( $row = $sth->fetchrow_hash( )){
    echo "Data: ".$row[column_name]."<br>\n";
  }
  $sth->finish( );
} else {
  echo "Could not prepare query: ".$sth->errstr."<br>\n";
}

//Insert some data...
$query = "INSERT INTO some_table VALUES( '1', 'some text' )";
$count = $dbh->dbh_do( $query );
if( $count ){
  echo "Inserted $count row.<br>\n";
} else {
  echo "Insert Failed: ".$dbh->errstr."<br>\n";
}

//Get the insert ID (assuming you have a serial column):
$serial = $dbh->insert_id( );


//Disconnect from the database.  Do not call
//  this if you are going for persistent connections or
//  you connection will no longer be persistent.
$dbh->disconnect( );



Functions
ClassFunctionDescription
DBI connect( ... ) Connect to the database. call it like connect( $dbi_string, $db_user, $db_passwd ) or connect( $db_type, $db_name, $db_host, $db_user, $db_passwd ). See the perl DBI man page or the sample code for example dbi_strings.
DBI disconnect( ) Disconnect from the database. Do not call this if you are trying to use persistent connections.
DBI prepare( $query ) Prepare a query. Returns an 'STH' object.
DBI dbh_do( $query ) Run the query. Returns the number of affected rows.
DBI insert_id( ) Returns the insert ID from the last "INSERT INTO..." called with dbh_do if you have a "AUTO_INCREMENT" column (in MySQL) defined.
DBI quote( $string ) Returns a properly quoted and escaped string for the database.

STH execute( ) Run the prepared query. Returns '1' on success.
STH rows( ) Returns the number of rows selected in the query. A note (summarized from the user contributed documentation for sybase_num_rows): Be careful when calling this function. Some databases (Sybase) do not know the number of rows before hand and will fetch all of the records for you to get this number. Informix will only return the number of records you have retrieved so far. Bottom line is read the *_num_rows documentation for your database and test it so you know what this funciton will return.
STH finish( ) Finishes (think *_free_result) the prepared query.
STH fetchrow_array( ) Fetch a row and return an array: each of the columns is accessed by position, e.g. $row[0].
STH fetchrow_hash( ) Fetch a row and return a hash a.k.a associative array: each of the columns is accessed by name, e.g. $row[column_name].


This page has only been accessed 3 times today by 3 hosts.
  And 13 times in the past seven days by 13 hosts during those seven days.

Disclaimer: All links and information is provided "As Is". A companies and/or product linked to or mentioned on this site does not represent an endorcment of said item. Use at your own risk. Send any questions, comments, complaints, etc. to: The Webmaster (Yes the nospam-... is a real email address)