Jump to content

adrix

Members
  • Posts

    1
  • Joined

  • Last visited

    Never
  • Donations

    0.00 GBP 

Posts posted by adrix

  1. I, sorry for my little english ... i speak italian ;)

    this is my 2cent; please adjust path for your directory structure.

    configuration.php

    <?php
    $dbHost = 'mysqlServerNameOrIPAddress';
    $dbUser = 'mysqlUserName';
    $dbPass = 'mysqlUserPassword';
    $dbName = 'mysqlDatabaseName';
    // Add here other var if necessary for your script
    ?>
    

    <?php

    autoloadClass.php

    <?php
    spl_autoload_register ( null, false );
    /* Warning: Using register extension only if necessary 
    spl_autoload_extensions ( '.php, .class.php, .inc' ); */
    /* Loader. Warning: Using or modify function name if necessary if you using various directory for class
                                   Example: commonClass, siteClass, adminClass
    */
    function commonClassLoader ( $className )
    {
       $filename = strtolower( $className ) . '.php';
       $file = [b]typeYourPath[/b] . '/common/' . strtolower($className) . '/' . $filename;
           if ( !file_exists ( $file ) )
           {       
               return false;
           }
           require_once $file;
    }
    ?>
    

    database.php

    <?php
    class Database
    {
    
           /**
            *      Database connections attribute
            */
           private $_dbHost;
           private $_dbUser;
           private $_dbPass;
           private $_dbName;
           private $_linkID;
           private $_selectDB;
    
            public function __construct ( $dbHost, $dbUser, $dbPass, $dbName ) 
            {
    
    
                   $this->_dbHost = $dbHost;
                   $this->_dbUser = $dbUser;
                   $this->_dbPass = $dbPass;
                   $this->_dbName = $dbName;
                   $this->_linkID = null;
                   $this->_selectDB = null;
    
                   /**
                    * Create connection.
                    */
                   $this->_linkID = mysql_connect ( $this->_dbHost, $this->_dbUser, $this->_dbPass);
    
                            if ( !$this->_linkID ) 
                            { 
                                   throw new Exception ( mysql_error() . ' : ' . mysql_errno() );                               
                            }
    
                   // Select db. 
                   $this->_selectDB = mysql_select_db ( $this->_dbName, $this->_linkID );
    
                           if ( !$this->_selectDB )
                           {
                                   throw new Exception ( mysql_error() . ' : ' . mysql_errno() );
                           }    
            }
    
           // Run a query
       public function dbQuery ( $sql )
       {
    
           $query = mysql_query ( $sql );
           if ( !$query )
           {
    
               throw new Exception ( mysql_error() . ' : ' . mysql_errno() );
    
           }
           return $query;
    
       }
    
       // Number of rows involved from query        
       public function dbAffectedRows ()
       {
    
              return mysql_affected_rows ();
    
       }
    
       // Last auto incremental id 
       public function dbInsertId ()
       {
    
              return mysql_insert_id ();
    
       }
    
       // Extract result set (
       public function dbFetchArray ( $result, $resultType )
       {
    
              switch ( $resultType ) {
                  case "mysql_num":
                      return mysql_fetch_array ( $result, MYSQL_NUM );
               case "mysql_assoc":
                   return mysql_fetch_array ( $result, MYSQL_ASSOC );
               default:
                   return mysql_fetch_array ( $result, MYSQL_BOTH );
    
           }
       }
    
       // Fetch object method. Using example: $rows->id - $rows->race ... etcetera 
       public function dbFetchObeject ( $result )
       {
    
           return mysql_fetch_object ( $result );
    
       }
    
       // Variant of dbFetchArray method
       public function dbFetchAssoc ( $result )
       {
    
           return mysql_fetch_assoc ( $result );
    
       }
    
       // Variant of dbFetchArray method
       public function dbFetchRow ( $query )
       {
    
           return mysql_fetch_row( $query );
    
       }
    
       // Number of rows in result set.
       public function dbNumRows ( $result )
       {
    
           return mysql_num_rows ( $result );
    
       }
    
    
    }
    ?>
    

    Example of usage:

    <?php
    try
    {
    error_reporting ( E_ALL );
    session_start ();
    require_once 'configuration.php';
    require_once 'autoloadClass.php';
    spl_autoload_register ('commonClassLoader');
    $db = new Database( $dbHost, $dbUser, $dbPass, $dbName );
    
    /**
    
    
                   Insert here all code for you project.
    
    */
    } catch (Exception $e) {
    
       echo $e->getMessage();
    
    }
    ?>
    

    Enjoy !!

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use