Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SQL
- {
- private $_connection;
- public function __construct( $arguments = null )
- {
- /**
- * Assign default credentials - you can also load and assign these from another file, to keep the SQL class clean and
- * do something like Config::getSQLCredentials();
- */
- $this->hostname = 'host';
- $this->username = 'user';
- $this->password = 'password';
- $this->database = 'database';
- // Overwrite default credentials, if any arguments has been passed
- if( $arguments !== null && is_array( $arguments ) && count( $arguments ) )
- {
- if( array_key_exists( 'hostname', $arguments ) )
- $this->hostname = $arguments[ 'hostname' ];
- if( array_key_exists( 'database', $arguments ) )
- $this->database = $arguments[ 'database' ];
- if( array_key_exists( 'username', $arguments ) )
- $this->username = $arguments[ 'username' ];
- if( array_key_exists( 'password', $arguments ) )
- $this->password = $arguments[ 'password' ];
- }
- $this->_connection = new \mysqli
- (
- $this->hostname,
- $this->username,
- $this->password,
- $this->database
- );
- }
- public function __destruct()
- {
- mysqli_close( $this->_connection );
- }
- public function execute( $query )
- {
- return $this->getConnection()->query( $query );
- }
- private function getConnection()
- {
- if ( !$this->_connection )
- die( 'Connection not initialized, aborting..' );
- return $this->_connection;
- }
- }
- // ---- MAIN ----
- $sql = new SQL();
- // OR
- $sql = new SQL(
- array
- (
- 'hostname' => 'asd',
- 'password' => 'asr2we99sdf'
- )
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement