Created
October 12, 2015 05:47
-
-
Save tilhom/8657b18c9178e83f2642 to your computer and use it in GitHub Desktop.
db
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Megagroup; | |
class Db extends \mysqli { | |
protected static $instance; | |
protected static $options = array(); | |
private function __construct() { | |
$o = self::$options; | |
// turn of error reporting | |
mysqli_report(MYSQLI_REPORT_OFF); | |
// connect to database | |
@parent::__construct(isset($o['host']) ? $o['host'] : 'localhost', | |
isset($o['user']) ? $o['user'] : 'root', | |
isset($o['pass']) ? $o['pass'] : 'crhfvkhfr', | |
isset($o['dbname']) ? $o['dbname'] : 'cbr', | |
isset($o['port']) ? $o['port'] : 3306, | |
isset($o['sock']) ? $o['sock'] : false ); | |
// check if a connection established | |
if( mysqli_connect_errno() ) { | |
throw new \exception(\mysqli_connect_error(), \mysqli_connect_errno()); | |
} | |
self::set_charset("utf8"); | |
} | |
public static function getInstance() { | |
if( !self::$instance ) { | |
self::$instance = new self(); | |
} | |
return self::$instance; | |
} | |
public static function setOptions( array $opt ) { | |
self::$options = array_merge(self::$options, $opt); | |
} | |
public function query($query) { | |
if( !$this->real_query($query) ) { | |
throw new \exception( $this->error, $this->errno ); | |
} | |
$result = new \mysqli_result($this); | |
return $result; | |
} | |
public function prepare($query) { | |
$stmt = new \mysqli_stmt($this, $query); | |
return $stmt; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment