CDbUtil.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace loyalsoft;
  3. // php + mysql fuction class
  4. //error_reporting(E_ALL & ~ E_NOTICE);
  5. /**
  6. * CDB工具类[APHP三大操作单元之一]
  7. * @author jgao
  8. */
  9. class CDbUtil
  10. {
  11. public $conn = 0;
  12. public function dbconn($dbhost, $dbport, $dbuser, $dbpw, $dbname)
  13. {
  14. // CLogUtil::output("数据库初始化: $dbhost,$dbport,$dbuser,$dbpw,$dbname");
  15. // DebugHelper::log2file($dbhost, $dbport, $dbname, $dbuser, $dbpw);
  16. $this->conn = mysqli_connect($dbhost, $dbuser, $dbpw, $dbname, $dbport, true);
  17. !$this->conn && $this->halt("Connect to MySQL failed");
  18. $serverinfo = mysqli_get_server_info($this->conn);
  19. if ($serverinfo > '4.1' && $GLOBALS['charset']) {
  20. mysqli_query($this->conn, "SET character_set_connection=" . $GLOBALS['charset'] . ",character_set_results=" . $GLOBALS['charset'] . ",character_set_client=binary");
  21. }
  22. if ($serverinfo > '5.0') {
  23. mysqli_query($this->conn, "SET sql_mode=''");
  24. }
  25. if ($dbname && !mysqli_select_db($this->conn, $dbname)) {
  26. $this->halt('Cannot use database');
  27. }
  28. }
  29. public function select_db($dbname)
  30. {
  31. if (!mysqli_select_db($this->conn, $dbname)) {
  32. $this->halt('Cannot use database');
  33. }
  34. }
  35. public function server_info()
  36. {
  37. return mysqli_get_server_info($this->conn);
  38. }
  39. public function insert_id()
  40. {
  41. $arr = $this->fetch_array('SELECT LAST_INSERT_ID() as id');
  42. return $arr["id"];
  43. }
  44. public function get_value($SQL, $offset = 0, $field = 0)
  45. {
  46. $rt = $this->fetch_result($SQL);
  47. if (isset($rt[$offset][$field])) {
  48. return $rt[$offset][$field];
  49. }
  50. return false;
  51. }
  52. /**
  53. * 执行查询
  54. * @param type $SQL
  55. * @param type $method
  56. * @param type $error
  57. * @return mixed
  58. */
  59. public function query($SQL, $method = null, $error = true)
  60. {
  61. $query = mysqli_query($this->conn, $SQL);
  62. !$query && $error && $this->halt('Query Error: ' . $SQL);
  63. return $query;
  64. }
  65. /**
  66. * 执行更新的时候加上事物逻辑
  67. * @param type $SQL
  68. * @return int errno
  69. */
  70. public function safeQuery($SQL)
  71. {
  72. mysql_query('start transaction');
  73. mysql_query('SET autocommit=0');
  74. $query = mysql_query($SQL, $this->conn);
  75. $err = mysql_errno();
  76. if ($err && !$query) {
  77. mysql_query('rollback');
  78. } else {
  79. mysql_query('commit');
  80. }
  81. mysql_query('SET autocommit=1');
  82. return $err;
  83. }
  84. /**
  85. * 将查询结果作为索引数组返回
  86. * @param string $SQL
  87. * @return array
  88. */
  89. public function fetch_row($SQL)
  90. {
  91. return $this->fetch_result($SQL, MYSQLI_NUM);
  92. }
  93. /**
  94. * 将查询结果作为关联数组返回
  95. * @param string $SQL
  96. * @return array
  97. */
  98. public function fetch_array($SQL)
  99. {
  100. return $this->fetch_result($SQL, MYSQLI_ASSOC);
  101. }
  102. /**
  103. * 从结果中取一行作为结果返回
  104. * @param string $SQL
  105. * @param int $result_type 1. 关联数组, 2. 索引数组, 3 both
  106. * @return type
  107. */
  108. public function fetch_result($SQL, $result_type = MYSQLI_BOTH)
  109. {
  110. $arr = array();
  111. $query = $this->query($SQL);
  112. $data = mysqli_fetch_array($query, $result_type);
  113. while ($data) {
  114. $arr[] = $data;
  115. $data = mysqli_fetch_array($query, $result_type);
  116. }
  117. $this->free_result();
  118. return $arr;
  119. }
  120. public function affected_rows()
  121. {
  122. return mysqli_affected_rows($this->conn);
  123. }
  124. /**
  125. * 调取结果集中行的数目
  126. * @param type $SQL
  127. * @return int
  128. */
  129. public function num_rows($SQL)
  130. {
  131. $query = $this->query($SQL);
  132. if (!is_bool($query)) {
  133. return mysqli_num_rows($query);
  134. }
  135. return 0;
  136. }
  137. public function num_fields($SQL)
  138. {
  139. $query = $this->query($SQL);
  140. return mysqli_num_fields($query);
  141. }
  142. public function escape_string($str)
  143. {
  144. return mysqli_real_escape_string($this->conn, $str);
  145. }
  146. public function free_result()
  147. {
  148. $void = func_get_args();
  149. foreach ($void as $query) {
  150. if (is_resource($query) && get_resource_type($query) === 'mysql result') {
  151. mysqli_free_result($query);
  152. }
  153. }
  154. unset($void);
  155. }
  156. public function close()
  157. {
  158. $this->free_result();
  159. return mysqli_close($this->conn);
  160. }
  161. public function halt($msg = null)
  162. {
  163. exit($msg . '<br /><br />' . mysqli_error($this->conn));
  164. }
  165. }