Ver Mensaje Individual
  #35 (permalink)  
Antiguo 13/08/2007, 17:09
Avatar de GatorV
GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 18 años, 8 meses
Puntos: 2135
Re: Pregunta de principiante...

Mira para que te des una idea te dejo una clase que usaba anteriormente (ahora cambie por PDO)

Código PHP:
<?php
/**
 * Class GeckoDB
 * 
 * @package com.geckowd.utils;
 * @author Christopher Valderrama <[email protected]>
 * @copyright Copyright (c) 2005
 * @version $Id$v1.0$26 Oct 2005
 * @access public
 **/
class GeckoDB {
    var 
$_server;        // @var string Private variable that holds the MySQL Server
    
var $_user;        // @var string Private variable that holds the MySQL User
    
var $_pass;        // @var string Private variable that holds the MySQL Password
    
var $_db;        // @var string Private variable that hodls the MySQL Database
    
var $_dbLink;        // @var resource Private variable that holds the MySQL Resource of the connection
    
var $_totalQuerys;    // @var integer Private variable that holds the Total number of Querys sent
    
var $_error;        // @var array Private variable that hodls all of the debug/error information
    
    /**
     * GeckoDB::GeckoDB()
     * 
     * This is the class constructor, it needs a complete array or it will fail upon
     * connecting, the array must have the following info:
     * + servidor a string with the database server
     * + usuario a string with the database user
     * + password a string with the database password
     * + db a string with the database to select (if auto)
     * 
     * @param array $dsn_arr A complete array holding the DSN info
     * @return GeckoDB a new instance of the GeckoDB Class
     * @access public
     **/
    
function GeckoDB($server$user$pass$db) {
        
$this->_server         $server;
        
$this->_user         $user;
        
$this->_pass         $pass;
        
$this->_db            $db;
        
$this->_totalQuerys    0;
        
$this->_error         = array();
    }
    
    
/**
     * GeckoDB::Create()
     * 
     * Returns a static instance of this class
     * 
     * @return GeckoDB a new instance of the GeckoDB Class
     * @access public
     **/
    
function Create() {
        static 
$GeckoDBInstance;
        global 
$GeckoDBSettings;
        
        if( !isset( 
$GeckoDBSettings ) ) {
            
trigger_error"GeckoDBSettigns not set!, check your config!"E_USER_ERROR );
        }
        
        if( !isset( 
$GeckoDBInstance ) && !is_object$GeckoDBInstance ) ) {
            
$GeckoDBInstance = new GeckoDB($GeckoDBSettings['server'], $GeckoDBSettings['user'], $GeckoDBSettings['password'], $GeckoDBSettings['database'] );
            if( 
$GeckoDBSettings['auto_connect'] ) $GeckoDBInstance->Connect();
        }
        
        return 
$GeckoDBInstance;
    }
    
    
/**
     * GeckoDB::Connect()
     * 
     * This function attemps to connect to a MySQL Server must be called after the constructor
     * @see GeckoDB::GeckoDB()
     * @access public
     * @param string $db (optional) the database to connect if not specified in the $dsn_arr variable
     * @return boolean if true, means the connection was succesfull (will fail upon failure)
     **/
    
function Connect$db '' ) {
        
$this->_dbLink = @mysql_connect"$this->_server""$this->_user""$this->_pass" );
        if( 
is_resource$this->_dbLink ) ) {
            
$this->_debug"Succesfully Connected, Host: " mysql_get_host_info$this->_dbLink ) . ", MySQL Version: " mysql_get_server_info$this->_dbLink ) );
            
            
$db = ( empty( $db ) ? $this->_db $db );
            
            if( !
mysql_select_db"$db"$this->_dbLink ) ) {
                
$this->_fatalError"Can't select that Database" );
                return 
false;
            }
            
            
$this->_debug"Actual DB: $db" );
            return 
true;
        } else {
            
$this->_fatalError"Error can't connect: {$this->_server}" );
            return 
false;
        }
    }
    
    function 
changeDB$db ) {
        if( !
$this->Connected() ) {
            
$this->_fatalError"Can't Change Database, no Connection" );
            return 
false;
        }
        
        if( !
mysql_select_db$db$this->_dbLink ) ) {
            
$this->_fatalError"Can't select that Database" );
            return 
false;
        }
        
        
$this->_debug"Actual DB: $db" );
        return 
true;
    }
    
    
/**
     * GeckoDB::Connected()
     * 
     * This function will test if the MySQL connection is still alive (and will try to reconnect)
     * if it fails it will show a error and will stop script.
     * 
     * @return boolean will return true if is connected or will fail if it can't connect
     * @access public
     **/
    
function Connected$autoRenew false ) {
        if( !
is_resource$this->_dbLink) ) {
            if( 
$autoRenew && !@mysql_ping$this->_dbLink ) ) {
                
$this->_fatalError"Couldn't reconnect" );
            }
            return 
false;
        }
        return 
true;
    }
        
    
/**
     * GeckoDB::_debug()
     * 
     * This function saves a message with the correct TimeStamp in the $_error array.
     * 
     * @param string $msg the message to save
     * @access private
     * @return 
     **/
    
function _debug$msg ) {
        
$this->_error[] = $msg;
    }
    
    
/**
     * GeckoDB::_fatalError()
     * 
     * This function will display a error message and the mysql error number if a fatal
     * error is encountered.
     * 
     * It will also mail the error message and the class log, to the specified adress
     * (you need to define $GeckoDBErrorMail at some point before initializing the class)
     * 
     * @param string $msg the message to show upon failure
     * @access private
     * @return 
     **/
    
function _fatalError$msg ) {
        global 
$GeckoDBErrorMail;
        
        if( 
is_resource$this->_dbLink ) ) {
            
$errNo mysql_errno$this->_dbLink );
            
$errMsg mysql_error$this->_dbLink );
        } else {
            
$errNo mysql_errno();
            
$errMsg mysql_error();
        }
        
$this->_debug$msg " (errNo: $errNo) MySQL Message: $errMsg" );
        
        if( !empty( 
$GeckoDBErrorMail ) ) {
            
ini_set'sendmail_from'"geckodberror@" $_SERVER['SERVER_NAME'] );
            
$headers =  "From: geckodberror@{$_SERVER['SERVER_NAME']}\r\n" .
                        
"Reply-To: webmaster@{$_SERVER['SERVER_NAME']}\r\n" .
                        
"X-Mailer: GeckoDB Engine";
            
$subject "Database Error";
            
$toMail "There is a database error($errNo): $errMsg\n\n";
            
$toMail .= "additonal data:\n\n";
            
$toMail .= implode"\n"$this->_error );
            
            @
mail$GeckoDBErrorMail$subject$toMail$headers );        
        }
        
        
$fatalMsg "There appears to be a Database Error, please click <a href=\"javascript:history.go(-1);\">back</a> and try again<br><br>";
        
$fatalMsg.= "<hr>MySQL Info:<br><textarea rows=\"5\" cols=\"40\">$msg ($errNo): $errMsg</textarea>";
        
        die( 
$fatalMsg );
    }
    
    
/**
     * GeckoDB::getLog()
     * 
     * This function will return the log of all the functions called in the
     * class, and will join them by the glue parameter "\n" is the default, 
     * you can use for example "<br />".
     * 
     * @param string $glue the parameter to join 
     * @access public
     * @return string the log in a string form
     **/
    
function getLog$glue "\n" ) {
        return 
implode$glue$this->_error );
    }
    
    
/**
     * GeckoDB::ListDBs()
     * 
     * This function will list the Databases in the current connection, can be called after Connect
     * @see GeckoDB::Connect()
     * 
     * @access public
     * @return array a array fulled of the databases in the connection
     **/
    
function ListDBs() {
        if( !
$this->Connected() ) {
            return 
false;
        }
        
        
$dblist = @mysql_list_dbs$this->_dbLink );
        
$dbs = array();
        while( 
$d mysql_fetch_row$dblist ) ) {
            
$dbs[] = $d[0];
        }
        
$this->_debug"dbs listed" );
        
$this->_totalQuerys++;
        return 
$dbs;
    }
    
    
/**
     * GeckoDB::ListTables()
     * 
     * This function will return a array with all of the current tables
     * in the active Databases must be called after Connect
     * @see GeckoDB::Connect()
     * @access public
     * @return array the list of the tables in the current db
     **/
    
function ListTables() {
        if( !
$this->Connected() ) {
            return 
false;
        }
        
        
$query "SHOW TABLES FROM {$this->_db}";
        
$result = @mysql_query$query );
        if( !
$result $this->_fatalError"Error listing tables" );
        
        
$tables = array();
        while( 
$table mysql_fetch_row$result ) ) {
            
$tables[] = $table[0];
        }
        
        
$this->_debug"tables in {$this->_db} listed" );
        
$this->_totalQuerys++;
        return 
$tables;
    }

    
/**
     * GeckoDB::Query()
     * 
     * This function is the core of the class, it can send a MySQL Query
     * and if it fails it will display the error and possible mail it.
     * 
     * This function can return a new GeckoDBResult object or a boolean
     * true if the query was sent.
     * 
     * @see GeckoDBResult::GeckoDBResult()
     * @see GeckoDB::_fatalError()
     * @access public
     * @param string $query the query to send to MySQL
     * @return mixed this function can return a new Object or a boolean
     **/
    
function Query$query ) {
        if( !
$this->Connected() ) {
            return 
false;
        }
        
        if( !
class_exists"GeckoDBResult" ) ) {
            
$this->_fatalError"GeckoDBResult class not defined" );
            return 
false;
        }
        
        
$this->_totalQuerys++;
        
$result = @mysql_query$query$this->_dbLink );
        if( !
$result $this->_fatalError"Error in query $query" );
        
        
$this->_debug("Sent a query: $query");
        if( 
is_resource$result ) ) {
            
$rObj = new GeckoDBResult$query$result );
        } else {
            
$rObj $result;
        }
        
        return 
$rObj;
    }
    
    function 
parseQuery$query$data ) {
        for( 
$i 0$i count$data ); $i++ ) {
            
$data[$i] = $this->_escapeString$data[$i] );
        }
        
        
$newarray array_merge( array( $query ), $data );
        return 
call_user_func_array"sprintf"$newarray );
    }

    function 
AffectedRows() {
        if( !
$this->Connected() ) {
            return 
false;
        }
        
        return 
mysql_affected_rows$this->_dbLink );
    }
    
    
    function 
getTotalQuerys() {
        return 
$this->_totalQuerys;
    }

    
    function 
LastInsertID() {
        return 
mysql_insert_id$this->_dbLink );
    }
    
    
    function 
_escapeString$value ) {
        
        if( 
get_magic_quotes_gpc() ) {
            
$value stripslashes$value );
        }
        
        if( !
is_numeric$value ) ) {
            
$value mysql_real_escape_string$value$this->_dbLink );
        }
        
        
$this->_debug"escaped: $value" );
        
        return 
$value;
    }
    
    
    function 
Disconnect() {
        
$this->_debug"connection closed" );
        @
mysql_close$this->_dbLink );
    }
}
?>