Php > Databases
PostGreSQL and MySQL 2 in 1 db Manager
PostGreSQL and MySQL 2 in 1 db Manager 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: 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 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 );
Php Codes
Algorithms
Arrays
Authentication
Calendar
Code Snippets
Programs
Content Manage
Contest Related
Cookies
Credit Cards
DBase Related
Databases
Date Time
Directories
E-Mail
Errors
File
File System
Forms
Handling
Graphics
HTML and PHP
Informix
Ingres
InterBase
LDAP
Look and Feel
Miscellaneous
MySQL
Other
PHP Classes
Searching
Navigation
Statistics
Strings
User Manage