Advertisement
Guest User

Untitled

a guest
Jan 15th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.96 KB | None | 0 0
  1.  
  2. class SQL
  3. {
  4.     private $_connection;
  5.  
  6.     public function __construct( $arguments = null )
  7.     {
  8.         /**
  9.          *  Assign default credentials - you can also load and assign these from another file, to keep the SQL class clean and
  10.          *  do something like Config::getSQLCredentials();
  11.          */
  12.         $this->hostname = 'host';
  13.         $this->username = 'user';
  14.         $this->password = 'password';
  15.         $this->database = 'database';
  16.  
  17.         // Overwrite default credentials, if any arguments has been passed
  18.         if( $arguments !== null && is_array( $arguments ) && count( $arguments ) )
  19.         {
  20.             if( array_key_exists( 'hostname', $arguments ) )
  21.                 $this->hostname = $arguments[ 'hostname' ];
  22.  
  23.             if( array_key_exists( 'database', $arguments ) )
  24.                 $this->database = $arguments[ 'database' ];
  25.  
  26.             if( array_key_exists( 'username', $arguments ) )
  27.                 $this->username = $arguments[ 'username' ];
  28.  
  29.             if( array_key_exists( 'password', $arguments ) )
  30.                 $this->password = $arguments[ 'password' ];
  31.         }
  32.        
  33.         $this->_connection = new \mysqli
  34.         (
  35.             $this->hostname,
  36.             $this->username,
  37.             $this->password,
  38.             $this->database
  39.         );
  40.     }
  41.  
  42.     public function __destruct()
  43.     {
  44.         mysqli_close( $this->_connection );
  45.     }
  46.  
  47.     public function execute( $query )
  48.     {
  49.         return $this->getConnection()->query( $query );
  50.     }
  51.  
  52.     private function getConnection()
  53.     {
  54.         if ( !$this->_connection )
  55.             die( 'Connection not initialized, aborting..' );
  56.  
  57.         return $this->_connection;
  58.     }
  59. }
  60.  
  61. // ---- MAIN ----
  62.  
  63. $sql = new SQL();
  64.  
  65. // OR
  66.  
  67. $sql = new SQL(
  68.                   array
  69.                   (
  70.                       'hostname' => 'asd',
  71.                       'password' => 'asr2we99sdf'
  72.                   )
  73.               );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement