Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define( "DB_HOSTNAME" , 0x20 );
- define( "DB_USERNAME" , 0x40 );
- define( "DB_PASSWORD" , 0x60 );
- class database
- {
- /* Configuration of the Database.
- *///¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
- const DEFAULT_PERSISTANT = true; // Will we connect with a persistant connection to the database by default?
- const DEFAULT_ACCOUNT = 0; // Upon creation of the databases this account will be loaded automatically if it exists.
- const DEFAULT_CONNECT = true; // Will we automatically connect to the database once this class is initiated?
- /* The Database System.
- *///¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
- private $pData = array(); // The data of all MySQL accounts.
- private $pAccount = null; // The current account of the MySQL user.
- public $mSQLs = array();
- public $mResults = array();
- public function isConnected()
- {
- return $this->pAccount['connected'] === true ? true : false;
- }
- public function isPersistant()
- {
- return $this->pAccount['persistant'] === true ? true : false;
- }
- public function changeAccount( $id, $persistant = true )
- {
- if( $this->disconnect() )
- {
- $this->pAccount = $this->pData[$id];
- }
- else
- {
- throw new Exception( 'MySQL: Could not change account from "' . $this->pAccount[DB_USERNAME] . '" to "' . $this->pData[$id][DB_USERNAME] . '".' );
- }
- return $this->connect( $persistant );
- }
- public function connect( $persistant = PERSISTANT )
- {
- if( !$this->isConnected() )
- {
- if( $persistant )
- {
- $this->pAccount['persistant'] = true;
- $this->pAccount['handle'] = @mysql_pconnect( $this->pAccount[DB_HOSTNAME], $this->pAccount[DB_USERNAME], $this->pAccount[DB_PASSWORD] );
- }
- else
- {
- $this->pAccount['persistant'] = false;
- $this->pAccount['handle'] = @mysql_connect( $this->pAccount[DB_HOSTNAME], $this->pAccount[DB_USERNAME], $this->pAccount[DB_PASSWORD] );
- }
- if( $this->pAccount['handle'] )
- {
- $this->pAccount['connected'] = true;
- return true;
- }
- else
- {
- throw new Exception( 'MySQL: Could not connect to "' . $this->pAccount[DB_HOSTNAME] . '" with "' . $this->pAccount[DB_USERNAME] . '".' );
- }
- }
- return false;
- }
- public function pConnect()
- {
- return $this->connect( true );
- }
- public function disconnect()
- {
- if( $this->isConnected() )
- {
- if ( mysql_close( $this->pAccount['handle'] ) )
- {
- $this->pAccount['connected'] = false;
- return true;
- }
- }
- return false;
- }
- public function select( $id = 0 )
- {
- if( $this->isConnected() )
- {
- $list = mysql_list_dbs( $this->pAccount['handle'] );
- while( $row = mysql_fetch_object( $list ) )
- {
- $this->pAccount['databases'][$row->Database] = $row->Database;
- }
- if( is_int( $id ) )
- {
- $this->pAccount['databases'] = array_values( $this->pAccount['databases'] );
- }
- if( array_key_exists( $id, $this->pAccount['databases'] ) )
- {
- if( $this->pAccount['link'] = mysql_select_db( $this->pAccount['databases'][$id], $this->pAccount['handle'] ) )
- {
- $this->pAccount['selected'] = true;
- return true;
- }
- else
- {
- throw new Exception( 'MySQL: Could not select database "' . $id . '" on "' . $this->pAccount[DB_HOSTNAME] . '".' );
- }
- }
- elseif( sizeOf( $this->pAccount['databases'] ) == 0 )
- {
- throw new Exception( 'MySQL: There are no databases to be selected on "' . $this->pAccount[DB_HOSTNAME] . '".' );
- }
- else
- {
- throw new Exception( 'MySQL: Unkown database select error on "' . $this->pAccount[DB_HOSTNAME] . '".' );
- }
- }
- else
- {
- try
- {
- $this->connect();
- try
- {
- $this->select( $id );
- }
- catch( Exception $e )
- {
- die( $e->getMessage() );
- }
- }
- catch( Exception $e )
- {
- die( $e->getMessage() );
- }
- }
- return false;
- }
- public function query( $sql )
- {
- if( $this->isConnected() )
- {
- if( !$result = @mysql_query( $sql ) )
- {
- throw new Exception( 'MySQL: Querry could not be executed: "' . mysql_error() . '".' );
- }
- else
- {
- $this->mSQLs[] = $sql;
- $this->mResults[] = $result;
- return $result;
- }
- }
- return false;
- }
- public function freeLastResult()
- {
- mysql_free_result( end( $this->mResults ) );
- }
- public function freeAllResults()
- {
- foreach( $this->mResults as $result )
- {
- mysql_free_result( $result );
- }
- }
- public function __construct( $data, $account = DEFAULT_ACCOUNT, $connect = DEFAULT_CONNECT, $pconnect = DEFAULT_PERSISTANT, $database = null )
- {
- if( isset( $data ) )
- {
- if( is_array( $data ) )
- {
- if( sizeOf( $data ) > 0 )
- {
- foreach( $data as $account => $details )
- {
- if( sizeOf( $details ) === 3 )
- {
- $data[$account][DB_HOSTNAME] = isset( $data[$account][DB_HOSTNAME] ) ? $data[$account][DB_HOSTNAME] : '';
- $data[$account][DB_USERNAME] = isset( $data[$account][DB_USERNAME] ) ? $data[$account][DB_USERNAME] : '';
- $data[$account][DB_PASSWORD] = isset( $data[$account][DB_PASSWORD] ) ? $data[$account][DB_PASSWORD] : '';
- }
- else
- {
- die( 'The database configuration "' . key( $account ) . '" is missing some key values.' );
- }
- }
- }
- else
- {
- die( 'The database configuration does not contain any accounts.' );
- }
- }
- else
- {
- die( 'The database configuration is not configured right.' );
- }
- }
- else
- {
- die( 'The database configuration is not configured right.' );
- }
- $this->pData = $data;
- $this->pAccount = $this->pData[$account];
- if( $connect )
- {
- if( $pconnect )
- {
- $this->connect( true );
- }
- else
- {
- $this->connect( false );
- }
- if( isset( $database ) )
- {
- $this->select( $database );
- }
- }
- }
- }
- ?>
- <?php
- // This part should be in a configuration system of the website.
- define( "DB_READ" , 0 );
- define( "DB_WRITE" , 1 );
- define( "DB_FULL" , 2 );
- $dbinfo = array
- (
- DB_READ => array
- (
- DB_HOSTNAME => 'localhost',
- DB_USERNAME => 'read',
- DB_PASSWORD => 'TheAxeEffect1987',
- ),
- DB_WRITE => array
- (
- DB_HOSTNAME => 'localhost',
- DB_USERNAME => 'write',
- DB_PASSWORD => 'TheAxeEffect1987',
- ),
- DB_FULL => array
- (
- DB_HOSTNAME => 'localhost',
- DB_USERNAME => 'full',
- DB_PASSWORD => 'TheAxeEffect1987',
- )
- );
- // $db = new database( $dbinfo[, DEFAULT_ACCOUNT [, AUTO_CONNECT [, USE_PCONNECT [, AUTO_SELECT_DATABASE )
- $db = new database( $dbinfo, DB_READ, true, true, 'test' );
- if( $db->changeAccount( DB_FULL ) )
- {
- $db->query( "SELECT * FROM test" );
- $db->freeAllResults();
- }
- else
- {
- echo 'false';
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement