Location: PHPKode > scripts > db_handle > db_handle/dbfuncs.php
<?
/************************************************************************/
/* BOBBISSON SAYS: PHP-NUKE does rule                                   */
/* ===========================                                          */
/* however some places need to be worked out in a more intelligent way  */
/************************************************************************/

/************************************************************************/
/* PHP-NUKE: Web Portal System                                          */
/* ===========================                                          */
/*                                                                      */
/* Copyright (c) 2002 by Francisco Burzi (hide@address.com)         */
/* http://phpnuke.org                                                   */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/

if (eregi("dbfuncs.php",$PHP_SELF)) {
    Header("Location: /");
    die();
}

define('E_NO_HANDLER','No active database handler exist.');
define('E_NULL_RESOURCE','Supplied resource is invalid');
define('E_DB_UNKNOWN','Database is not supported by the driver.');
define('E_CONNECT_FAILED','Connection to Database failed.');
define('E_BAD_QUERY','There was an error processing query');

// global variable: DO NOT REDEFINE
$_db_environment = array();

// list of supported databases
$_db_supported = array(
    "MySQL",
    "mSQL",
    "PostgreSQL",
    "PostgreSQL_local",
    "ODBC",
    "ODBC_Adabas",
    "Interbase",
    "Sybase"
);

//  class ResultSet : for PostgreSQL
class ResultSet {
    var $result;
    var $total_rows;
    var $fetched_rows;

    function set_result( $res ) {
        $this->result = $res;
    }

    function get_result() {
        return $this->result;
    }

    function set_total_rows( $rows ) {
        $this->total_rows = $rows;
    }

    function get_total_rows() {
        return $this->total_rows;
    }

    function set_fetched_rows( $rows ) {
        $this->fetched_rows = $rows;
    }

    function get_fetched_rows() {
        return $this->fetched_rows;
    }

    function increment_fetched_rows() {
        $this->fetched_rows = $this->fetched_rows + 1;
    }
}

// data storage
class db_handle {
    var $handle;
    var $query;

    // params
    var $type;
    var $host;
    var $db;
    var $user;
    var $params;

    function db_handle($type,$host,$db,$user,$password,$params = false) {
        global $_db_supported;

        $this->handle = false;
        $this->query = array();
        if(!in_array($type,$_db_supported)) db_error(E_DB_UNKNOWN);
        $this->type = $type;
        $this->host = $host;
        $this->db = $db;
        $this->user = $user;
        $this->password = $password;
        $this->params = $params;
    }
}

/*
 * get_handler(&$arr)
 * returns current db handle
 */
function get_db_handler(&$argv) {
    global $_db_environment;
    if(is_array($argv) && (count($argv) > 0) && array_key_exists($argv[0],$_db_environment)) 
        return array_shift($argv);
    else 
        return $_db_environment['default'];
}


/*
 * set_default_dbh($name)
 * assumes $name current default db handle
 */
function set_default_dbh($name) {
    global $_db_environment;
    $_db_environment['default'] = $name;
}

/*
 * db_connect($type,$host,$db,$user,$password,$params)
 * returns the connection ID
 */
function db_connect($type,$host,$db,$user,$password,$params = false)
{
    global $_db_environment;
    $new_key = md5($type.$db.$user);
    $_db_environment['default'] = $new_key;
    if(array_key_exists($new_key,$_db_environment)) return $new_key;

    $dbh = new db_handle($type,$host,$db,$user,$password,$params);

    switch ($dbh->type) {
        case "MySQL":
            $dbh->handle = @mysql_connect($host,$user,$password);
            mysql_select_db($db);
        break;;

        case "mSQL":
            $dbh->handle = @msql_connect($host);
            msql_select_db($db);
        break;;

        case "PostgreSQL":
            $dbh->handle = @pg_connect("host=$host user=$user password=$password port=5432 dbname=$db");
        break;;

        case "PostgreSQL_local":
            $dbh->handle = @pg_connect("user=$user password=$password dbname=$db");
        break;;

        case "ODBC":
            $dbh->handle = @odbc_connect($db,$user,$password);
        break;;

        case "ODBC_Adabas":
            $dbh->handle = @odbc_connect($host.":".$db,$user,$password);
        break;;

        case "Interbase":
            if(is_array($params)) {
                $charset = $params['CHARSET'];
            }
            $dbh->handle = @ibase_pconnect($host.":".$db,$user,$password,$charset);
        break;;

        case "Sybase":
            $dbh->handle  = @sybase_connect($host, $user, $password);
            sybase_select_db($db,$dbh->handle);
        break;;

        default:
            return false;
        break;;
    }
    if(!$dbh->handle) {
        db_error(E_CONNECT_FAILED);
        return false;
    }
    $_db_environment[$new_key] = $dbh;
    return $new_key;
}

/*
 * db_logout([$dbh])
 * returns [god knows what]
 */
function db_logout()
{
    global $_db_environment;
    $argv = func_get_args();
    $dbh = $_db_environment[get_db_handler($argv)];
    if(!$dbh) db_error(E_NO_HANDLER);

    switch ($dbh->type) {
        case "MySQL":
            $dbi = @mysql_close($dbh->handle);
            return $dbi;
        break;;

        case "mSQL":
            $dbi = @msql_close($dbh->handle);
            return $dbi;
        break;;

        case "PostgreSQL":
        case "PostgreSQL_local":
            $dbi = @pg_close($dbh->handle);
            return $dbi;
        break;;
    
        case "ODBC":
        case "ODBC_Adabas":
            $dbi = @odbc_close($dbh->handle);
            return $dbi;  
        break;;

        case "Interbase":
            $dbi = @ibase_close($dbh->handle);
            return $dbi;
        break;;

        case "Sybase":
            $dbi = @sybase_close($dbh->handle);
            return $dbi;
        break;;
    
        default:
        break;;
    }
}

/*
 *
 *
 */
function db_error() {
    global $_db_environment;
    $argv = func_get_args();
    $dbh = $_db_environment[get_db_handler($argv)];

    echo join("\n",$argv);
    if(!$dbh) return;

    switch($dbh->type) {

        case "MySQL":
            echo @mysql_error($dbh->handle);
        break;;

        case "mSQL":
            echo @msql_error($dbh->handle);
        break;;

        case "PostgreSQL":
        case "PostgreSQL_local":
            echo @pg_last_error($dbh->handle);
        break;;

        case "ODBC":
        case "ODBC_Adabas":
            echo @odbc_errormsg($dbh->handle);
        break;;

        case "Interbase":
            echo @ibase_errmsg();
        break;;

        case "Sybase":
            echo @sybase_get_last_message();
        break;;

        default:
        break;;
    }
}

/* 
 * db_query([$dbh, ][$tag, ]$query)
 * executes an SQL statement, returns a result identifier
 */
  
function db_query()
{
    global $_db_environment;
    $argv = func_get_args();
    $key = get_db_handler($argv);
    if($key == '') db_error(E_NO_HANDLER);

    if((count($argv) > 1) && (eregi('^[a-z_][0-9a-z_]+$',$argv[0]))) $tag = array_shift($argv);
    else $tag = 'default';
    $query = array_shift($argv);

    global $db_debug;
    $db_debug = 0;
    if($db_debug) echo "SQL query: $query<br>";
    $dbh = $_db_environment[$key]->handle;
    switch($_db_environment[$key]->type) {

        case "MySQL":
            $res = @mysql_query($query,$dbh);
        break;;

        case "mSQL":
            $res = @msql_query($query,$dbh);
        break;;

        case "PostgreSQL":
        case "PostgreSQL_local":
            $res=pg_exec($dbh,$query);
            $result_set = new ResultSet;
            $result_set->set_result($res);
            $result_set->set_total_rows( db_num_rows( $result_set ) );
            $result_set->set_fetched_rows( 0 ); 
        break;;

        case "ODBC":
        case "ODBC_Adabas":
            $res = @odbc_exec($dbh,$query);
        break;;

        case "Interbase":
            $res = @ibase_query($dbh,$query);
        break;;

        case "Sybase":
            $res = @sybase_query($query,$dbh);
        break;;

        default:
        break;;
    }
    if(!$res) db_error();
    $_db_environment[$key]->query[$tag] = $res;

    return $res;
}       
        
/*  
 * db_num_rows($res) KILLED !!! terrible function please do not use it ever...
 * It can prove quite memory-consuming
 */  

/*
 * db_fetch_row([$dbh, ][$tag, ]$nr)           
 * given a result tag identifier, returns an array with the resulting row  
 * Needs also a row number for ODBC (huh?)
 */                                  
                                     
function db_fetch_row()    
{                                    
    global $_db_environment;
    $argv = func_get_args();
    $key = get_db_handler($argv);
    if($key == '') db_error(E_NO_HANDLER);

    if((count($argv) > 0) && is_string($argv[0])) $tag = array_shift($argv);
    else $tag = 'default';

    $res = $_db_environment[$key]->query[$tag];
    
    if(!$res) db_error(E_NULL_RESOURCE);

    if(count($argv) > 0) $nr  = array_shift($argv);

    switch ($_db_environment[$key]->type) {
                                        
        case "MySQL":
            $row = mysql_fetch_row($res);
        break;;                          
                                        
        case "mSQL":                     
            $row = msql_fetch_row($res);
        break;;                          
                                        
        case "PostgreSQL":               
        case "PostgreSQL_local":
            if($res->get_total_rows() <= $res->get_fetched_rows()) return false;

            $row = pg_fetch_row($res->get_result(), $res->get_fetched_rows() );
            $res->increment_fetched_rows();
        break;;                          
                                        
        case "ODBC":                     
        case "ODBC_Adabas":
            $row = array();              
            $cols = odbc_fetch_into($res,$nr,$row);
        break;;                          
                                        
        case "Interbase":
            $row = ibase_fetch_row($res);
        break;;                          

        case "Sybase":
            $row = sybase_fetch_row($res);
        break;;                          

        default:                         
        break;;                          
    }
    return $row;
}

/*                                   
 * db_fetch_array([$dbh, ][$tag, ]$nr)           
 * given a result identifier, returns an associative array             
 * with the resulting row using field names as keys.                   
 * Needs also a row number for compatibility with PostgreSQL.          
 */                                  
                                     
function db_fetch_array()  
{                                    
    global $_db_environment;
    $argv = func_get_args();
    $key = get_db_handler($argv);
    if($key == '') db_error(E_NO_HANDLER);


    if((count($argv) > 0) && is_string($argv[0])) $tag = array_shift($argv);
    else $tag = 'default';

    $res = $_db_environment[$key]->query[$tag];
    
    if(!$res) db_error(E_NULL_RESOURCE);

    if(count($argv) > 0) $nr  = array_shift($argv);

    switch ($_db_environment[$key]->type) {
        case "MySQL":                    
            $row = array();              
            $row = mysql_fetch_array($res);
        break;;                          
                                        
        case "mSQL":                     
            $row = array();              
            $row = msql_fetch_array($res);
            return $row;                 
        break;;                          
                                        
        case "PostgreSQL":               
        case "PostgreSQL_local":
        if( $res->get_total_rows > $res->get_fetched_rows() ) {
            $row = array();              
            $row = pg_fetch_array($res->get_result(), $res->get_fetched_rows() );
            $res->increment_fetched_rows();
            return $row;                 
        } else {
            return false;
        }
        break;;                          
                                        
/*                                   
 * ODBC doesn't have a native _fetch_array(), so we have to            
 * use a trick. Beware: this might cause HUGE loads!                   
 */                                  
                                        
        case "ODBC":                     
            $row = array();              
            $result = array();           
            $result = odbc_fetch_row($res, $nr);                           
            $nf = odbc_num_fields($res); /* Field numbering starts at 1 */
            for($count=1; $count < $nf+1; $count++) 
            {
                $field_name = odbc_field_name($res, $count);               
                $field_value = odbc_result($res, $field_name);             
                $row[$field_name] = $field_value;                          
            }                        
        break;;                          

        case "ODBC_Adabas":                     
            $row = array();              
            $result = array();           
            $result = odbc_fetch_row($res, $nr);                           

            $nf = count($result)+2; /* Field numbering starts at 1 */
            for($count=1; $count < $nf; $count++) {
                $field_name = odbc_field_name($res, $count);
                $field_value = odbc_result($res, $field_name);
                $row[$field_name] = $field_value;
            }
        break;;                          

        case "Interbase":
            $orow=ibase_fetch_object($res);
            $row=get_object_vars($orow);
        break;;                          

        case "Sybase":
            $row = sybase_fetch_array($res);
        break;;                          

    }
    return $row;
}

/*
 * db_fetch_row([$dbh, ][$tag, ]$nr)
 * given th eresult identifier, returns objects with columns as attributes
 */
function db_fetch_object()
{                                    
    global $_db_environment;
    $argv = func_get_args();
    $key = get_db_handler($argv);
    if($key == '') db_error(E_NO_HANDLER);


    if((count($argv) > 0) && is_string($argv[0])) $tag = array_shift($argv);
    else $tag = 'default';

    $res = $_db_environment[$key]->query[$tag];
    
    if(!$res) db_error(E_NULL_RESOURCE);

    if(count($argv) > 0) $nr  = array_shift($argv);

    switch ($_db_environment[$key]->type) {

        case "MySQL":                    
            $row = mysql_fetch_object($res);
        break;;                          
                                        
        case "mSQL":                     
            $row = msql_fetch_object($res);
        break;;                          
                                        
        case "PostgreSQL":
        case "PostgreSQL_local":
        if( $res->get_total_rows > $res->get_fetched_rows() ) {
            $row = pg_fetch_object( $res->get_result(), $res->get_fetched_rows() );
            $res->increment_fetched_rows();
        } else {
            return false;
        }
        break;;

        case "ODBC":                     
            $result = odbc_fetch_row($res, $nr);                       
            if(!$result) return false;
            $nf = odbc_num_fields($res); /* Field numbering starts at 1 */
            for($count=1; $count < $nf+1; $count++) 
            {                        
                $field_name = odbc_field_name($res, $count);
                $field_value = odbc_result($res, $field_name);             
                $row->$field_name = $field_value;
            }                        
        break;;                          

        case "ODBC_Adabas":                     
            $result = odbc_fetch_row($res, $nr);                           
            if(!$result) return false;

            $nf = count($result)+2; /* Field numbering starts at 1 */
            for($count=1; $count < $nf; $count++) {
                $field_name = odbc_field_name($res, $count);
                $field_value = odbc_result($res, $field_name);
                $row->$field_name = $field_value;
            }
        break;;                          

        case "Interbase":
            $orow = ibase_fetch_object($res);
            if($orow)
            {
                $arow=get_object_vars($orow);
                while(list($name,$key)=each($arow))
                {
                    $name=strtolower($name);
                    $row->$name=$key;
                }
            }else return false;
        break;;                          

        case "Sybase":
            $row = sybase_fetch_object($res);
        break;;                          
    }                                
    return $row;
}

/*
 * db_free_result($res)
 * frees query result
 */
function db_free_result() {
    global $_db_environment;
    $argv = func_get_args();
    $key = get_db_handler($argv);
    if($key == '') db_error(E_NO_HANDLER);


    if((count($argv) > 0) && is_string($argv[0])) $tag = array_shift($argv);
    else $tag = 'default';

    $res = $_db_environment[$key]->query[$tag];
    
    if(!$res) db_error(E_NULL_RESOURCE);

    switch ($_db_environment[$key]->type) {

        case "MySQL":
            $row = @mysql_free_result($res);
        break;; 
        
        case "mSQL":
            $row = @msql_free_result($res);
        break;; 

        case "PostgreSQL":
        case "PostgreSQL_local":
            $row = @pg_FreeResult( $res->get_result() );
        break;;
            
        case "ODBC":
        case "ODBC_Adabas":
            $row = @odbc_free_result($res);
        break;;
            
        case "Interbase":
            $row = @ibase_free_result($res);
        break;;

        case "Sybase":
            $row = @sybase_free_result($res);
        break;;
    }
    if(!$row) db_error(E_BAD_QUERY);
    return $row;
}

?>
Return current item: db_handle