Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class db{
- public $mysqli;
- private $total_queries = 0;
- protected $result = false;
- public function __construct($type){
- if ($this->mysqli->connect_error){
- exit('Unable to establish a connection to your database. Please verify your settings are correct.');
- }
- }
- public function query($sql, $binds = false){
- if ($binds){
- $sql = $this->_binds($sql, $binds);
- }
- if (($this->result = $this->mysqli->query($sql)) === false){
- exit("Invalid query : ".$this->mysqli->error);
- return false;
- }
- return true;
- }
- public function select($select, $table, $where = '', $binds = '', $orderby = '', $order = 'ASC', $limit = 0){
- $sql = 'SELECT ';
- if (!is_array($select)){
- $select = array($select);
- }
- $i = 0;
- foreach ($select as $v){
- $sql .= ($i == 0) ? '' : ',';
- $sql .= $v;
- ++$i;
- }
- $sql .= ' FROM ';
- $sql .= $table;
- if (!empty($where)){
- $sql .= ' WHERE '.$where;
- }
- if (!empty($orderby)){
- $sql .= ' ORDER BY '.$orderby.' '.$order;
- }
- if ($limit > 0 || !empty($limit)){
- $sql .= ' LIMIT '.$limit;
- }
- return $this->query($sql, $binds);
- }
- public function join($type = '', $select, $tables, $on, $where = '', $binds = '', $orderby = '', $order = 'ASC', $limit = 0){
- $tableSet = '';
- $key = 'a';
- foreach ($tables as $table){
- $tableSet .= DB_PREFIX.$table.' AS '.$key;
- if ($key == 'a'){
- $key = 'b';
- $tableSet .= ' '.$type.' JOIN ';
- }else{
- $tableSet .= ' ON '.$on;
- }
- }
- return $this->select($select, $tableSet, $where, $binds, $orderby, $order, $limit);
- }
- public function result($type = 'object'){
- return ($type == 'array') ? $this->fetch_array() : $this->fetch_object();
- }
- public function insert($table, $args){
- $keys = '';
- $values = '';
- $i = count($args);
- $x = 0;
- foreach ($args as $k => $v){
- ++$x;
- $comma = ($x < $i) ? ',' : '';
- $keys .= $k . $comma;
- $values .= $this->cleanse($v) . $comma;
- }
- $sql = 'INSERT INTO '.DB_PREFIX.$table.'('.$keys.') VALUES('.$values.')';
- return $this->query($sql);
- }
- public function update($table, $args = array(), $where = array()){
- $update = '';
- $i = count($args);
- $x = 0;
- foreach ($args as $k => $v){
- ++$x;
- $comma = ($x < $i) ? ',' : '';
- $update .= $k.'=\''.$this->_cleanse($v).'\''.$comma;
- }
- $sql = 'UPDATE '.DB_PREFIX.$table.' SET '.$update.' WHERE';
- $and = false;
- foreach ($where as $k => $v){
- if ($and) $sql .= ' AND';
- $sql .= ' '.$k.'=\''.$this->_cleanse($v).'\'';
- $and = true;
- }
- return $this->query($sql);
- }
- public function delete($table, $where, $binds = array()){
- $sql = 'DELETE FROM '.DB_PREFIX.$table.' WHERE '.$where;
- return $this->query($sql, $binds);
- }
- private function fetch_object(){
- return $this->result->fetch_object();
- }
- private function fetch_array(){
- while (($result = $this->result->fetch_assoc())){
- $array[] = $result;
- }
- return $array;
- }
- public function num_rows(){
- return $this->result->num_rows;
- }
- public function insert_id(){
- return $this->mysqli->insert_id;
- }
- private function _binds($sql, $binds){
- if (!is_array($binds)){
- $binds = array($binds);
- }
- $segments = explode('?', $sql);
- if (count($binds) >= count($segments)){
- $binds = array_slice($binds, 0, count($segments) - 1);
- }
- $sql = $segments[0];
- $i = 0;
- foreach ($binds as $bind){
- $sql .= $this->cleanse($bind);
- $sql .= $segments[++$i];
- }
- return $sql;
- }
- private function cleanse($str){
- if (is_string($str)){
- $str = "'".$this->_cleanse($str)."'";
- }
- elseif (is_bool($str)){
- $str = ($str === false) ? 0 : 1;
- }else{
- $str = ($str === NULL) ? 'NULL' : $str;
- }
- return $str;
- }
- private function _cleanse($str){
- if (function_exists('mysqli_real_escape_string')){
- $str = $this->mysqli->real_escape_string($str);
- }else{
- $str = addslashes($str);
- }
- return $str;
- }
- public function close(){
- $this->mysqli->close();
- }
- }
Add Comment
Please, Sign In to add comment