<?php
/**
 * [UQcms System] Copyright uqcms.com
 * QQ: 2200631718
 * Author: gj.name
 * This is NOT a freeware
*/
 class dbi { function __construct($db_cfg){ $this->host = $db_cfg['mysql']['DB_HOST']; $this->user = $db_cfg['mysql']['DB_USER']; $this->pwd = $db_cfg['mysql']['DB_PWD']; $this->name = $db_cfg['mysql']['DB_NAME']; } function conn(){ $this->conn = new mysqli($this->host, $this->user, $this->pwd, $this->name) or die('<h1>数据库没有链接上</h1>'); $this->conn->query("SET names UTF8"); } function query($sql){ if(empty($sql)){ return false; } $t1 = microtime(true); $query = $this->conn->query($sql); $use_time = microtime(true) - $t1; if($use_time >= '0.1'){ db_log(date('Y-m-d H:i:s').' SLOW: '.$sql); } if($this->conn->error){ echo $this->conn->error; echo '<br>'; echo $sql.'<br>'; db_log(date('Y-m-d H:i:s').' ERROR: '.$sql); return false; } if($query){ return $query; }else{ return false; } } function fetch_array($result){ return $result->fetch_array(MYSQLI_ASSOC); } function add($table,$data){ $keys = ""; $values = ""; foreach($data as $key =>$val){ if(trim($val) !== ''){ $keys .= "`".$key."`,"; if (!get_magic_quotes_gpc()){ $values .= " '".addslashes($val)."',"; }else{ $values .= " '".$val."',"; } } } $keys=rtrim($keys,","); $values=rtrim($values,","); $sql = "INSERT INTO `{$table}` (".$keys.") VALUES  (".$values.");"; return $this->query($sql); } function update($table,$data,$where,$echo_sql = '0'){ $values=""; foreach($data as $key=>$val){ if(trim($val) !== ''){ if (!get_magic_quotes_gpc()){ $values .= "`".$key."` = '".addslashes($val)."',"; }else{ $values .= "`".$key."` = '".$val."',"; } }else{ $values .= "`".$key."` = NULL,"; } } $values=rtrim($values,","); if($echo_sql == '1'){ echo "UPDATE `{$table}` SET ".$values." where {$where} "; }else{ $sql = "UPDATE `{$table}` SET ".$values." where {$where} "; } return $this->query($sql); } function get_one($sql){ $query = $this->query($sql); if($query){ $data = $query->fetch_array(MYSQLI_ASSOC); if($data){ return $data; }else{ return true; } }else{ return false; } } function get_all($sql){ $query = $this->query($sql); if($query){ while($row = $query->fetch_array(MYSQLI_ASSOC)){ $data[] = $row; } if(isset($data)){ return $data; }else{ return false; } } } public function insert_id() { return $this->conn->insert_id; } public function delete($table, $condition = "", $echo_sql = '0'){ if(empty($condition)) { return false; } $sql = "delete from $table where 1=1 and $condition"; if($echo_sql){ echo $sql; } if(!$this->query($sql)) return false; return true; } public function autocommit(){ $this->conn->autocommit(false); } public function commit(){ $this->conn->commit(); } public function rollback(){ $this->conn->rollback(); } public function autocommit_free_result(){ $this->conn->autocommit(); } public function free_result() { $void = func_get_args(); foreach($void as $query) { if(is_resource($query) && get_resource_type($query) === 'mysql result') { return $this->conn->free_result($query); } } } public function close() { $this->conn->close; } }