My universal DB PHP Connection and Query classes

Website development, web apps, desktop/server apps, mobile apps and any other coding
Locked
halojoy
Posts: 19
Joined: Thu Jun 09, 2005 9:18 am

My universal DB PHP Connection and Query classes

Post by halojoy »

My db connector class supports Most common databases:
MySQL, MySQLi, SQLite, MsSQL, PostgreSQL, Oracle ...
As can be seen from alternatives in my connector class:
case 'mysql':
case 'mysqli':
case 'sqlite':
case 'mssql':       
case 'pgsql':
case 'oci8':
case 'ibm_db2':
This is a very short practical example howto use it:

Code: Select all

<?php
require_once('db.php');
$db = db::connect();

$res = $db->do_query("SELECT * FROM try1users");
if($db->errstr)
    echo $db->errstr;

echo '<pre>';
var_dump($res);
echo '</pre>';

?>[code] 

[color=#888888][size=85]Archived topic from Iceteks,  old topic ID:4990, old post ID:38665[/size][/color]
... i make php ... i make it good
halojoy
Posts: 19
Joined: Thu Jun 09, 2005 9:18 am

My universal DB PHP Connection and Query classes

Post by halojoy »

:)

This is the basic setup of my Connector Class
where specific driver is loaded:

Code: Select all

<?php

class db {

    // Constructor not used
    private function __construct(){
    }
    
    // Connect database driver $dbtype to one database $dbname
    public static function connect(){

        require_once('db_config.php');
        $dbtype = isset($dbtype) ? trim($dbtype) : '';
        $dbhost = isset($dbhost) ? trim($dbhost) : '';
        $dbuser = isset($dbuser) ? trim($dbuser) : '';
        $dbpass = isset($dbpass) ? trim($dbpass) : '';
        $dbname = isset($dbname) ? trim($dbname) : '';
        $dbmode = isset($dbmode) ? trim($dbmode) : '';
        $dbport = isset($dbport) ? trim($dbport) : '';
        $dbfile = isset($dbfile) ? trim($dbfile) : '';
        $lib = 'db_lib/';

        // Prepare for connect of db drivers
        switch($dbtype){
            case 'mysql':
                if($dbport){
                    $dbhost = ($dbport==3306) ? $dbhost : $dbhost.':'.$dbport;
                }
                require_once($lib.'db_mysql_test.php');
                $db = new db_mysql($dbhost,$dbuser,$dbpass,$dbname);    
            break;
            
            case 'mysqli':
                $dbport = ($dbport==3306) ? null : (int)$dbport;
                require_once($lib.'db_mysqli.php');
                $db = new db_mysqli($dbhost,$dbuser,$dbpass,$dbname,$dbport);    
            break;

            case 'sqlite':
                require_once($lib.'db_sqlite.php');
                $db = new db_sqlite($dbfile);    
            break;
 
            case 'mssql':
                if($dbport){
                    $dbhost = ($dbport==1433) ? $dbhost : $dbhost.','.$dbport;
                }
                require_once($lib.'db_mssql.php');
                $db = new db_mssql($dbhost,$dbuser,$dbpass,$dbname);    
            break;
            
            case 'pgsql':
                require_once($lib.'db_pgsql.php');
                $db = new db_pgsql($dbhost,$dbuser,$dbpass,$dbname,$dbport);    
            break;
            
            case 'oci8':
                require_once($lib.'db_oci8.php');
                $db = new db_oci8($dbhost,$dbuser,$dbpass,$dbname,$dbport);    
            break;

            case 'ibm_db2':
                require_once($lib.'db_ibm_db2.php');
                $db = new db_ibm_db2($dbhost,$dbuser,$dbpass,$dbname,$dbport);    
            break;
            
            default:
                exit('Error: Invalid config value: $dbtype ');
            break;

        }

        return $db; // Database driver handle

    } // end connect()

} // end class 

?>[code] 

[color=#888888][size=85]Archived topic from Iceteks,  old topic ID:4990, old post ID:38666[/size][/color]
... i make php ... i make it good
halojoy
Posts: 19
Joined: Thu Jun 09, 2005 9:18 am

My universal DB PHP Connection and Query classes

Post by halojoy »

:)

If you would like a ZIP download of my DB Connect and Query Execute
just tell me so, and I upload or give a download link,
for free open source .

Any of your comments are of course very welcome.
Cheers all Mates and PHP-Lovers in Europe and elsewhere.

/halojoy - sweden January 28th 2008.

Archived topic from Iceteks, old topic ID:4990, old post ID:38667
... i make php ... i make it good
Locked