Ok so in this example, I'm showing you a class that is mixture of PostGreSQL and MYSQL. It's a combined class, and all the functions are contemporary to both the dbs( Escept the last function .. get_lastid() loyal to PostGreSQL ). As usual, this class is followed by an example.
sql.php:
class database
author...: leapinglangoor [ leapinglangoor@yahoo.co.in ]
version..: v.17
Updated..: 12 Aug 2004
Note.....: This script is 2 in 1 Portsgre cum MySQL
script that can be used just like any other
database manager.
CLASS DATABASE COMES WITHOUT ANY WARRANTY
USE AT YOUR OWN RISK
<?php
class SQL
{
var $type ;
var $dbconn ;
var $host , $port , $dbname , $user ;
var $TYPE_PGSQL , $TYPE_MYSQL ;
function Database ( $type )
{
$this -> TYPE_PGSQL = "pgsql" ;
$this -> TYPE_MYSQL = "mysql" ;
}
function create ( $type )
{
$this -> type = $type ;
}
function open ( $host , $port , $dbname , $user , $password )
{
$this -> host = $host ;
$this -> port = $port ;
$this -> dbname = $dbname ;
$this -> user = $user ;
if ( ! strcmp ( $this -> type , $this -> TYPE_PGSQL ) )
$this -> dbconn = pg_pconnect ( "host= $host port= $port dbname= $dbname user= $user password= $password " );
else if ( ! strcmp ( $this -> type , $this -> TYPE_MYSQL ) )
$this -> dbconn = mysql_pconnect ( " $host : $port " , $user , $password );
if ( ! $this -> dbconn ) return false ;
else return true ;
}
function close ()
{
if ( ! strcmp ( $this -> type , $this -> TYPE_PGSQL ) )
return pg_close ( $this -> dbconn );
else if ( ! strcmp ( $this -> type , $this -> TYPE_MYSQL ) )
return mysql_close ( $this -> dbconn );
}
function exec ( $sql )
{
if ( ! strcmp ( $this -> type , $this -> TYPE_PGSQL ) )
return pg_exec ( $this -> dbconn , $sql );
else if ( ! strcmp ( $this -> type , $this -> TYPE_MYSQL ) )
return mysql_db_query ( $this -> dbname , $sql , $this -> dbconn );
}
function numrows ( $query )
{
if ( ! strcmp ( $this -> type , $this -> TYPE_PGSQL ) )
return pg_numrows ( $query );
else if ( ! strcmp ( $this -> type , $this -> TYPE_MYSQL ) )
return mysql_num_rows ( $query );
}
function fetch_array ( $query , $row )
{
if ( ! strcmp ( $this -> type , $this -> TYPE_PGSQL ) )
return pg_fetch_array ( $query , $row );
else if ( ! strcmp ( $this -> type , $this -> TYPE_MYSQL ) )
{
mysql_data_seek ( $query , $row );
return mysql_fetch_array ( $query );
}
}
function fetch_object ( $query , $row )
{
if ( ! strcmp ( $this -> type , $this -> TYPE_PGSQL ) )
return pg_fetch_object ( $query , $row );
else if ( ! strcmp ( $this -> type , $this -> TYPE_MYSQL ) )
{
mysql_data_seek ( $query , $row );
return mysql_fetch_object ( $query );
}
}
function get_lastid ( $query , $table , $field )
{
if ( ! strcmp ( $this -> type , $this -> TYPE_PGSQL ) )
{
$oid = pg_getlastoid ( $query );
$qaux = pg_exec ( "select $field from $table where oid= $oid " );
$arr = pg_fetch_array ( $qaux , 0 );
return $arr [ $field ];
}
}
}
?>
Here is the example to go with the class...
example.php
<?php
include( 'sql.php' );
// Create an instance of the class.
$db = new SQL ();
/*
@Param - $type
$type should either be
'pgsql' for PosGreSQL or
'myssql' for MYSQL
There wont be any diffrence in the methods of accessing data depending on the type of db chosen. Exception in the case of the last function - get_lastid();
*/
$type = 'pgsql' ;
$db -> create ( $type );
// Lets open a connection to the db
$db -> open ( 'localhost' , 3306 , 'database-to-open' , 'user' , 'password' );
$sql = 'SELECT * FROM `some_table`' ;
$result = $db -> exec ( $sql );
/*
------ Work on the result ---------
-- You can use $db->numrows, $db->fetch_array and so on...
-- Check MySQL and PostGreSQL manuals for more information...
/*
// Lets close the connection...
$db->close();
?>
Setting up InnoDB on MySQL and using Transactions Begin, Commit, Rollback in PHP. Categories : PHP Classes , Databases , PHP , MySQL , InnoDB This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server. Categories : Databases , MySQL , Complete Programs , PHP , Databases [PHP5] aDB PDO LIKE Database Abstraction. Switch easily from one db server to another, strong errors management, manage transactions, queries preparation and more. Categories : PHP , PHP Classes , Databases , MS SQL Server , MySQL Link Manager for Link Exchangers Categories : PHP , PHP Classes , Databases , MySQL , CURL Specify your connection settings and create a link to a MySQL database. Categories : PHP , PHP Classes , Databases , MySQL , Beginner Guides Simple database class Categories : PHP , PHP Classes , MySQL , Databases A script to generate a report from a valid mysql connection. The user has to supply which fields he wants to display in table. All properties are changable.
Categories : PHP , PHP Classes , Databases , MySQL , HTML and PHP MySQL Handler Categories : PHP , Databases , MySQL , Classes and Objects , PHP Classes Convert SQL from oracle,mysql,mssql,sqlite and odbc to SQL compatible Categories : PHP , PHP Classes , Databases , MySQL , MS SQL Server Simple Mini Poll class library (SimPoll) Categories : PHP , PHP Classes , Databases , MySQL , Complete Programs Ajax PHP Tree (Left and Right) with MySQL Categories : PHP , Databases , MySQL , AJAX , PHP Classes YellowPages Content Grabber (PHP5 +) Categories : PHP , PHP Classes , Regexps , Databases , MySQL Simple usersOnline class - keep track of how many users are online on your site Categories : PHP , PHP Classes , Databases , MySQL This is Yet Another Sql Abstraction Library. Include it in your script and you can use the most important SQL functions without worrying about the SQL backend. Categories : Databases , PHP , ODBC , MySQL , PostgreSQL PHP Object Example of the Perl DBI with MySQL Categories : PHP , PHP Classes , MySQL , Databases , Perl