conn = mysqli_connect($dbhost, $dbuser, $dbpw, $dbname, $dbport, true); !$this->conn && $this->halt("Connect to MySQL failed"); $serverinfo = mysqli_get_server_info($this->conn); if ($serverinfo > '4.1' && $GLOBALS['charset']) { mysqli_query($this->conn, "SET character_set_connection=" . $GLOBALS['charset'] . ",character_set_results=" . $GLOBALS['charset'] . ",character_set_client=binary"); } if ($serverinfo > '5.0') { mysqli_query($this->conn, "SET sql_mode=''"); } if ($dbname && !mysqli_select_db($this->conn, $dbname)) { $this->halt('Cannot use database'); } } /** * 切换db * @param type $dbname */ public function select_db($dbname) { if (!mysqli_select_db($this->conn, $dbname)) { $this->halt('Cannot use database'); } } /** * 调取服务器信息 * @return type */ public function server_info() { return mysqli_get_server_info($this->conn); } /** * 最后一次执行insert时插入的第一行记录Id,(如果一次插入3条记录,则得到第一条的记录) * 所以,这个如何用要慎重(gwang) * @return type */ public function insert_id() { $arr = $this->fetch_array('SELECT LAST_INSERT_ID() as id'); return $arr["id"]; } /** * 直接取查询结果的某行某列的数值 * @param type $SQL * @param type $offset * @param type $field * @return boolean */ public function get_value($SQL, $offset = 0, $field = 0) { $rt = $this->fetch_result($SQL); if (isset($rt[$offset][$field])) { return $rt[$offset][$field]; } return false; } /** * 执行查询 * @param type $SQL * @param type $method * @param type $error * @return mixed */ public function query($SQL, $method = null, $error = true) { $query = mysqli_query($this->conn, $SQL); !$query && $error && $this->halt('Query Error: ' . $SQL); return $query; } /** * 执行更新的时候加上事物逻辑 * @param type $SQL * @return int errno */ public function safeQuery($SQL) { mysqli_query($this->conn, 'start transaction'); mysqli_query($this->conn, 'SET autocommit=0'); $query = mysqli_query($this->conn, $SQL); $err = mysqli_errno($this->conn); if ($err && !$query) { mysqli_query($this->conn, 'rollback'); } else { mysqli_query($this->conn, 'commit'); } mysqli_query($this->conn, 'SET autocommit=1'); return $err; } /** * 将查询结果作为索引数组返回 * @param string $SQL * @return array */ public function fetch_row($SQL) { return $this->fetch_result($SQL, MYSQLI_NUM); } /** * 将查询结果作为关联数组返回 * @param string $SQL * @return array */ public function fetch_array($SQL) { return $this->fetch_result($SQL, MYSQLI_ASSOC); } /** * 从结果中取一行作为结果返回 * @param string $SQL * @param int $result_type 1. 关联数组, 2. 索引数组, 3 both * @return type */ public function fetch_result($SQL, $result_type = MYSQLI_BOTH) { $arr = array(); $query = $this->query($SQL); $data = mysqli_fetch_array($query, $result_type); while ($data) { $arr[] = $data; $data = mysqli_fetch_array($query, $result_type); } $this->free_result(); return $arr; } /** * 调取查询影响的行数 * @return type */ public function affected_rows() { return mysqli_affected_rows($this->conn); } /** * 调取结果集中行的数目 * @param type $SQL * @return int */ public function num_rows($SQL) { $query = $this->query($SQL); if (!is_bool($query)) { return mysqli_num_rows($query); } return 0; } /** * 调取结果集中字段数量 * @param type $SQL * @return type */ public function num_fields($SQL) { $query = $this->query($SQL); return mysqli_num_fields($query); } /** * 封装SQL字符串中特殊字符,以免SQL语句执行失败. * @param type $str 带有风险字符的SQL语句或者字符串 * @return type 处理后的字符串 */ public function escape_string($str) { return mysqli_real_escape_string($this->conn, $str); } /** * 释放SQL查询结果 */ public function free_result() { $void = func_get_args(); foreach ($void as $query) { if (is_resource($query) && get_resource_type($query) === 'mysql result') { mysqli_free_result($query); } } unset($void); } /** * 关闭当前实例的连接 * @return type */ public function close() { $this->free_result(); return mysqli_close($this->conn); } /** * 直接报错退出 * @param type $msg */ public function halt($msg = null) { exit($msg . '

' . mysqli_error($this->conn)); } }