Advertisement
Guest User

Untitled

a guest
May 7th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.03 KB | None | 0 0
  1. <?php
  2.  
  3.     define( "DB_HOSTNAME"   , 0x20 );
  4.     define( "DB_USERNAME"   , 0x40 );
  5.     define( "DB_PASSWORD"   , 0x60 );
  6.  
  7.     class database
  8.     {
  9.         /*  Configuration of the Database.
  10.         *///¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
  11.         const   DEFAULT_PERSISTANT = true;          // Will we connect with a persistant connection to the database by default?
  12.         const   DEFAULT_ACCOUNT    = 0;             // Upon creation of the databases this account will be loaded automatically if it exists.
  13.         const   DEFAULT_CONNECT    = true;          // Will we automatically connect to the database once this class is initiated?
  14.  
  15.            
  16.         /*  The Database System.
  17.         *///¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
  18.         private $pData             = array();       // The data of all MySQL accounts.
  19.         private $pAccount          = null;          // The current account of the MySQL user.
  20.        
  21.         public  $mSQLs             = array();
  22.         public  $mResults          = array();
  23.    
  24.        
  25.        
  26.        
  27.         public function isConnected()
  28.         {
  29.             return $this->pAccount['connected']  === true ? true : false;
  30.         }
  31.         public function isPersistant()
  32.         {
  33.             return $this->pAccount['persistant'] === true ? true : false;
  34.         }
  35.         public function changeAccount( $id, $persistant = true )
  36.         {
  37.             if( $this->disconnect() )
  38.             {
  39.                 $this->pAccount = $this->pData[$id];
  40.             }
  41.             else
  42.             {
  43.                 throw new Exception( 'MySQL: Could not change account from "' . $this->pAccount[DB_USERNAME] . '" to "' . $this->pData[$id][DB_USERNAME] . '".' );
  44.             }
  45.            
  46.             return $this->connect( $persistant );
  47.         }
  48.        
  49.         public function connect( $persistant = PERSISTANT )
  50.         {
  51.             if( !$this->isConnected() )
  52.             {
  53.                 if( $persistant )
  54.                 {
  55.                     $this->pAccount['persistant'] = true;
  56.                     $this->pAccount['handle']     = @mysql_pconnect( $this->pAccount[DB_HOSTNAME], $this->pAccount[DB_USERNAME], $this->pAccount[DB_PASSWORD] );
  57.                 }
  58.                 else
  59.                 {
  60.                     $this->pAccount['persistant'] = false;
  61.                     $this->pAccount['handle']     = @mysql_connect( $this->pAccount[DB_HOSTNAME], $this->pAccount[DB_USERNAME], $this->pAccount[DB_PASSWORD] );
  62.                 }
  63.                
  64.                 if( $this->pAccount['handle'] )
  65.                 {
  66.                     $this->pAccount['connected'] = true;
  67.                    
  68.                     return true;
  69.                 }
  70.                 else
  71.                 {
  72.                     throw new Exception( 'MySQL: Could not connect to "' . $this->pAccount[DB_HOSTNAME] . '" with "' . $this->pAccount[DB_USERNAME] . '".' );
  73.                 }
  74.             }
  75.            
  76.             return false;
  77.         }
  78.         public function pConnect()
  79.         {
  80.             return $this->connect( true );
  81.         }
  82.         public function disconnect()
  83.         {
  84.             if( $this->isConnected() )
  85.             {
  86.                 if ( mysql_close( $this->pAccount['handle'] ) )
  87.                 {
  88.                     $this->pAccount['connected'] = false;
  89.                    
  90.                     return true;
  91.                 }
  92.             }
  93.            
  94.             return false;
  95.         }
  96.         public function select( $id = 0 )
  97.         {
  98.             if( $this->isConnected() )
  99.             {
  100.                 $list = mysql_list_dbs( $this->pAccount['handle'] );
  101.                
  102.                 while( $row = mysql_fetch_object( $list ) )
  103.                 {
  104.                     $this->pAccount['databases'][$row->Database] = $row->Database;
  105.                 }
  106.                
  107.                 if( is_int( $id ) )
  108.                 {
  109.                     $this->pAccount['databases'] = array_values( $this->pAccount['databases'] );
  110.                 }
  111.                
  112.                
  113.                 if( array_key_exists( $id, $this->pAccount['databases'] ) )
  114.                 {
  115.                     if( $this->pAccount['link'] = mysql_select_db( $this->pAccount['databases'][$id], $this->pAccount['handle'] ) )
  116.                     {
  117.                         $this->pAccount['selected'] = true;
  118.                    
  119.                         return true;
  120.                     }
  121.                     else
  122.                     {
  123.                         throw new Exception( 'MySQL: Could not select database "' . $id . '" on "' . $this->pAccount[DB_HOSTNAME] . '".' );
  124.                     }
  125.                 }
  126.                 elseif( sizeOf( $this->pAccount['databases'] ) == 0 )
  127.                 {
  128.                     throw new Exception( 'MySQL: There are no databases to be selected on "' . $this->pAccount[DB_HOSTNAME] . '".' );
  129.                 }
  130.                 else
  131.                 {
  132.                     throw new Exception( 'MySQL: Unkown database select error on "' . $this->pAccount[DB_HOSTNAME] . '".' );
  133.                 }              
  134.             }
  135.             else
  136.             {
  137.                 try
  138.                 {
  139.                     $this->connect();
  140.                     try
  141.                     {
  142.                         $this->select( $id );
  143.                     }
  144.                     catch( Exception $e )
  145.                     {
  146.                         die( $e->getMessage() );
  147.                     }
  148.                 }
  149.                 catch( Exception $e )
  150.                 {
  151.                     die( $e->getMessage() );
  152.                 }
  153.             }
  154.            
  155.             return false;
  156.         }
  157.         public function query( $sql )
  158.         {
  159.             if( $this->isConnected() )
  160.             {
  161.                 if( !$result = @mysql_query( $sql ) )
  162.                 {
  163.                     throw new Exception( 'MySQL: Querry could not be executed: "' . mysql_error() . '".' );
  164.                 }
  165.                 else
  166.                 {
  167.                     $this->mSQLs[]    = $sql;
  168.                     $this->mResults[] = $result;
  169.                    
  170.                     return $result;
  171.                 }
  172.             }
  173.            
  174.             return false;
  175.         }
  176.         public function freeLastResult()
  177.         {
  178.             mysql_free_result( end( $this->mResults ) );
  179.         }
  180.         public function freeAllResults()
  181.         {
  182.             foreach( $this->mResults as $result )
  183.             {
  184.                 mysql_free_result( $result );
  185.             }
  186.         }
  187.        
  188.        
  189.         public function __construct( $data, $account = DEFAULT_ACCOUNT, $connect = DEFAULT_CONNECT, $pconnect = DEFAULT_PERSISTANT, $database = null )
  190.         {  
  191.             if( isset( $data ) )
  192.             {
  193.                 if( is_array( $data ) )
  194.                 {
  195.                     if( sizeOf( $data ) > 0 )
  196.                     {
  197.                         foreach( $data as $account => $details )
  198.                         {
  199.                             if( sizeOf( $details ) === 3 )
  200.                             {
  201.                                 $data[$account][DB_HOSTNAME] = isset( $data[$account][DB_HOSTNAME] ) ? $data[$account][DB_HOSTNAME] : '';
  202.                                 $data[$account][DB_USERNAME] = isset( $data[$account][DB_USERNAME] ) ? $data[$account][DB_USERNAME] : '';
  203.                                 $data[$account][DB_PASSWORD] = isset( $data[$account][DB_PASSWORD] ) ? $data[$account][DB_PASSWORD] : '';
  204.                             }
  205.                             else
  206.                             {
  207.                                 die( 'The database configuration "' . key( $account ) . '" is missing some key values.' );
  208.                             }
  209.                         }
  210.                     }
  211.                     else
  212.                     {
  213.                         die( 'The database configuration does not contain any accounts.' );
  214.                     }                  
  215.                 }
  216.                 else
  217.                 {
  218.                     die( 'The database configuration is not configured right.' );
  219.                 }
  220.             }
  221.             else
  222.             {
  223.                 die( 'The database configuration is not configured right.' );
  224.             }
  225.            
  226.             $this->pData    = $data;
  227.             $this->pAccount = $this->pData[$account];
  228.            
  229.             if( $connect )
  230.             {
  231.                 if( $pconnect )
  232.                 {
  233.                     $this->connect( true );
  234.                 }
  235.                 else
  236.                 {
  237.                     $this->connect( false );
  238.                 }
  239.                 if( isset( $database ) )
  240.                 {
  241.                     $this->select( $database );
  242.                 }
  243.             }
  244.         }
  245.     }
  246.  
  247. ?>
  248.  
  249.  
  250. <?php
  251.  
  252.     // This part should be in a configuration system of the website.
  253.  
  254.     define( "DB_READ"   , 0 );
  255.     define( "DB_WRITE"  , 1 );
  256.     define( "DB_FULL"   , 2 );
  257.    
  258.     $dbinfo = array
  259.     (
  260.         DB_READ => array
  261.         (
  262.             DB_HOSTNAME => 'localhost',
  263.             DB_USERNAME => 'read',
  264.             DB_PASSWORD => 'TheAxeEffect1987',
  265.         ),
  266.        
  267.         DB_WRITE => array
  268.         (
  269.             DB_HOSTNAME => 'localhost',
  270.             DB_USERNAME => 'write',
  271.             DB_PASSWORD => 'TheAxeEffect1987',
  272.         ),
  273.  
  274.         DB_FULL => array
  275.         (
  276.             DB_HOSTNAME => 'localhost',
  277.             DB_USERNAME => 'full',
  278.             DB_PASSWORD => 'TheAxeEffect1987',
  279.         )
  280.     );
  281.    
  282. //  $db = new database( $dbinfo[, DEFAULT_ACCOUNT [, AUTO_CONNECT [, USE_PCONNECT [, AUTO_SELECT_DATABASE )
  283.     $db = new database( $dbinfo, DB_READ, true, true, 'test' );
  284.    
  285.     if( $db->changeAccount( DB_FULL ) )
  286.     {
  287.         $db->query( "SELECT * FROM test" );
  288.         $db->freeAllResults();
  289.     }
  290.     else
  291.     {
  292.         echo 'false';
  293.     }
  294.        
  295. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement