123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- namespace loyalsoft;
- /**
- * CDB工具类[APHP三大操作单元之一]
- * @version 2.0.1 gwang 修补了部分mysqli方法 2020.3.25
- * 2.0 gwang 升级到mysqli,以便能在PHP7.0以后的环境下继续运行. 约 2017
- * 1.0 jgao created 约 2013
- * @author jgao,gwang
- */
- class CDbUtil {
- public $conn = 0;
- public function dbconn($dbhost, $dbport, $dbuser, $dbpw, $dbname) {
- // CLogUtil::output("数据库初始化: $dbhost,$dbport,$dbuser,$dbpw,$dbname");
- // DebugHelper::log2file($dbhost, $dbport, $dbname, $dbuser, $dbpw);
- $this->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 . '<br /><br />' . mysqli_error($this->conn));
- }
- }
|