CDbUtil.php 5.1 KB

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