123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- namespace loyalsoft;
- // php + mysql fuction class
- //error_reporting(E_ALL & ~ E_NOTICE);
- /**
- * CDB工具类[APHP三大操作单元之一]
- * @author jgao
- */
- 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');
- }
- }
- public function select_db($dbname)
- {
- if (!mysqli_select_db($this->conn, $dbname)) {
- $this->halt('Cannot use database');
- }
- }
- public function server_info()
- {
- return mysqli_get_server_info($this->conn);
- }
- public function insert_id()
- {
- $arr = $this->fetch_array('SELECT LAST_INSERT_ID() as id');
- return $arr["id"];
- }
- 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)
- {
- mysql_query('start transaction');
- mysql_query('SET autocommit=0');
- $query = mysql_query($SQL, $this->conn);
- $err = mysql_errno();
- if ($err && !$query) {
- mysql_query('rollback');
- } else {
- mysql_query('commit');
- }
- mysql_query('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;
- }
- 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;
- }
- public function num_fields($SQL)
- {
- $query = $this->query($SQL);
- return mysqli_num_fields($query);
- }
- public function escape_string($str)
- {
- return mysqli_real_escape_string($this->conn, $str);
- }
- 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);
- }
- public function close()
- {
- $this->free_result();
- return mysqli_close($this->conn);
- }
- public function halt($msg = null)
- {
- exit($msg . '<br /><br />' . mysqli_error($this->conn));
- }
- }
|