Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * DB Class
- */
- class DB
- {
- private static $_instance;
- private $_connection;
- private $_dbhost = DB_HOST;
- private $_dbuser = DB_USER;
- private $_dbpass = DB_PASS;
- private $_dbname = DB_NAME;
- private $_dbcharset = DB_CHARSET;
- public static function getInstance()
- {
- if (!self::$_instance) {
- self::$_instance = new self();
- }
- return self::$_instance;
- }
- private function __construct()
- {
- $this->_connection = new mysqli($this->_dbhost, $this->_dbuser, $this->_dbpass, $this->_dbname);
- if (mysqli_connect_errno()) {
- trigger_error('Database connection failed: ' . mysqli_connect_error(), E_USER_ERROR);
- }
- $this->_connection->set_charset($this->_dbcharset);
- }
- function getConnection()
- {
- return $this->_connection;
- }
- function query($sql)
- {
- $args = func_get_args();
- $sql = array_shift($args);
- $link = $this->_connection;
- $args = array_map(function ($param) use ($link) {
- return $link->escape_string($param);
- }, $args);
- $sql = str_replace(array('%', '?'), array('%%', '%s'), $sql);
- array_unshift($args, $sql);
- $sql = call_user_func_array('sprintf', $args);
- $this->_query = $this->_connection->query($sql);
- if($this->_query === false) throw new Exception('Database error: ' . $this->_connection->error);
- return $this;
- }
- function changeDb($db_name)
- {
- $this->_connection->select_db($db_name);
- }
- function data_seek($offset = 0)
- {
- return $this->_query->data_seek($offset);
- }
- function field_seek($field)
- {
- return $this->_query->field_seek($field);
- }
- function insert_id()
- {
- return $this->_query->insert_id;
- }
- function fetch_field_direct($field)
- {
- return $this->_query->fetch_field_direct($field);
- }
- function assoc()
- {
- return $this->_query->fetch_assoc();
- }
- function fetch_field()
- {
- while ($row = $this->_query->fetch_field()) $_result[] = $row->name;
- return $_result;
- }
- function fetch_row()
- {
- return $this->_query->fetch_row();
- }
- function fetch_object($class_name = "stdClass", $param = null)
- {
- if(is_null($param))
- {
- return $this->_query->fetch_object($class_name);
- }
- else
- {
- return $this->_query->fetch_object($class_name, $param);
- }
- }
- function all()
- {
- while($row = $this->_query->fetch_assoc()) $_result[] = $row;
- return $_result;
- }
- function __destruct()
- {
- $this->_connection->close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement