Mega Code Archive

 
Categories / Php / MySQL Database
 

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: <?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 );