|
|
|
|
|
|
| |
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 );
| | |
|
| MySQL Handler Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes | | | Powerful php/mysql Pagination for up to 6 URL Params Categories : PHP, PHP Classes, Databases, MySQL, Navigation | | | Password reminder Categories : PHP, PHP Classes, Databases, MySQL, Mail | | | MySQL Class to ease Database connectivity Categories : MySQL, PHP Classes, Databases, PHP | | | usercounter class Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables | | | Simple Mini Poll class library (SimPoll) Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs | | | Simple database class Categories : PHP, PHP Classes, MySQL, Databases | | | Simple usersOnline class - keep track of how many users are online on your site Categories : PHP, PHP Classes, Databases, MySQL | | | Online Automatic Class Generator for MySQL Tables Categories : PHP, PHP Classes, Classes and Objects, Databases, MySQL | | | DBE - Database Expander: Edit PostgreSQL individual database tables online via your Web browser! Categories : PostgreSQL, Complete Programs, Databases, PHP Classes, PHP | | | Specify your connection settings and create a link to a MySQL database. Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides | | | Setting up InnoDB on MySQL and using Transactions Begin, Commit, Rollback in PHP. Categories : PHP Classes, Databases, PHP, MySQL, InnoDB | | | 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 | | | MySQL Connection/Query Class Categories : Databases, MySQL, PHP, PHP Classes | |
|
|
|