CDbUtil.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace loyalsoft;
  3. /**
  4. * CDB工具类[APHP三大操作单元之一]
  5. * @version 2.0.1 gwang 修补了部分mysqli方法 2020.3.25
  6. * 2.0 gwang 升级到mysqli,以便能在PHP7.0以后的环境下继续运行. 约 2017
  7. * 1.0 jgao created 约 2013
  8. * @author jgao,gwang
  9. */
  10. class CDbUtil {
  11. public $conn = 0;
  12. public function dbconn($dbhost, $dbport, $dbuser, $dbpw, $dbname) {
  13. // CLogUtil::output("数据库初始化: $dbhost,$dbport,$dbuser,$dbpw,$dbname");
  14. // DebugHelper::log2file($dbhost, $dbport, $dbname, $dbuser, $dbpw);
  15. $this->conn = mysqli_connect($dbhost, $dbuser, $dbpw, $dbname, $dbport, true);
  16. !$this->conn && $this->halt("Connect to MySQL failed");
  17. $serverinfo = mysqli_get_server_info($this->conn);
  18. if ($serverinfo > '4.1' && $GLOBALS['charset']) {
  19. mysqli_query($this->conn, "SET character_set_connection=" . $GLOBALS['charset'] . ",character_set_results=" . $GLOBALS['charset'] . ",character_set_client=binary");
  20. }
  21. if ($serverinfo > '5.0') {
  22. mysqli_query($this->conn, "SET sql_mode=''");
  23. }
  24. if ($dbname && !mysqli_select_db($this->conn, $dbname)) {
  25. $this->halt('Cannot use database');
  26. }
  27. }
  28. /**
  29. * 切换db
  30. * @param type $dbname
  31. */
  32. public function select_db($dbname) {
  33. if (!mysqli_select_db($this->conn, $dbname)) {
  34. $this->halt('Cannot use database');
  35. }
  36. }
  37. /**
  38. * 调取服务器信息
  39. * @return type
  40. */
  41. public function server_info() {
  42. return mysqli_get_server_info($this->conn);
  43. }
  44. /**
  45. * 最后一次执行insert时插入的第一行记录Id,(如果一次插入3条记录,则得到第一条的记录)
  46. * 所以,这个如何用要慎重(gwang)
  47. * @return type
  48. */
  49. public function insert_id() {
  50. $arr = $this->fetch_array('SELECT LAST_INSERT_ID() as id');
  51. return $arr["id"];
  52. }
  53. /**
  54. * 直接取查询结果的某行某列的数值
  55. * @param type $SQL
  56. * @param type $offset
  57. * @param type $field
  58. * @return boolean
  59. */
  60. public function get_value($SQL, $offset = 0, $field = 0) {
  61. $rt = $this->fetch_result($SQL);
  62. if (isset($rt[$offset][$field])) {
  63. return $rt[$offset][$field];
  64. }
  65. return false;
  66. }
  67. /**
  68. * 执行查询
  69. * @param type $SQL
  70. * @param type $method
  71. * @param type $error
  72. * @return mixed
  73. */
  74. public function query($SQL, $method = null, $error = true) {
  75. $query = mysqli_query($this->conn, $SQL);
  76. !$query && $error && $this->halt('Query Error: ' . $SQL);
  77. return $query;
  78. }
  79. /**
  80. * 执行更新的时候加上事物逻辑
  81. * @param type $SQL
  82. * @return int errno
  83. */
  84. public function safeQuery($SQL) {
  85. mysqli_query($this->conn, 'start transaction');
  86. mysqli_query($this->conn, 'SET autocommit=0');
  87. $query = mysqli_query($this->conn, $SQL);
  88. $err = mysqli_errno($this->conn);
  89. if ($err && !$query) {
  90. mysqli_query($this->conn, 'rollback');
  91. } else {
  92. mysqli_query($this->conn, 'commit');
  93. }
  94. mysqli_query($this->conn, 'SET autocommit=1');
  95. return $err;
  96. }
  97. /**
  98. * 将查询结果作为索引数组返回
  99. * @param string $SQL
  100. * @return array
  101. */
  102. public function fetch_row($SQL) {
  103. return $this->fetch_result($SQL, MYSQLI_NUM);
  104. }
  105. /**
  106. * 将查询结果作为关联数组返回
  107. * @param string $SQL
  108. * @return array
  109. */
  110. public function fetch_array($SQL) {
  111. return $this->fetch_result($SQL, MYSQLI_ASSOC);
  112. }
  113. /**
  114. * 从结果中取一行作为结果返回
  115. * @param string $SQL
  116. * @param int $result_type 1. 关联数组, 2. 索引数组, 3 both
  117. * @return type
  118. */
  119. public function fetch_result($SQL, $result_type = MYSQLI_BOTH) {
  120. $arr = array();
  121. $query = $this->query($SQL);
  122. $data = mysqli_fetch_array($query, $result_type);
  123. while ($data) {
  124. $arr[] = $data;
  125. $data = mysqli_fetch_array($query, $result_type);
  126. }
  127. $this->free_result();
  128. return $arr;
  129. }
  130. /**
  131. * 调取查询影响的行数
  132. * @return type
  133. */
  134. public function affected_rows() {
  135. return mysqli_affected_rows($this->conn);
  136. }
  137. /**
  138. * 调取结果集中行的数目
  139. * @param type $SQL
  140. * @return int
  141. */
  142. public function num_rows($SQL) {
  143. $query = $this->query($SQL);
  144. if (!is_bool($query)) {
  145. return mysqli_num_rows($query);
  146. }
  147. return 0;
  148. }
  149. /**
  150. * 调取结果集中字段数量
  151. * @param type $SQL
  152. * @return type
  153. */
  154. public function num_fields($SQL) {
  155. $query = $this->query($SQL);
  156. return mysqli_num_fields($query);
  157. }
  158. /**
  159. * 封装SQL字符串中特殊字符,以免SQL语句执行失败.
  160. * @param type $str 带有风险字符的SQL语句或者字符串
  161. * @return type 处理后的字符串
  162. */
  163. public function escape_string($str) {
  164. return mysqli_real_escape_string($this->conn, $str);
  165. }
  166. /**
  167. * 释放SQL查询结果
  168. */
  169. public function free_result() {
  170. $void = func_get_args();
  171. foreach ($void as $query) {
  172. if (is_resource($query) && get_resource_type($query) === 'mysql result') {
  173. mysqli_free_result($query);
  174. }
  175. }
  176. unset($void);
  177. }
  178. /**
  179. * 关闭当前实例的连接
  180. * @return type
  181. */
  182. public function close() {
  183. $this->free_result();
  184. return mysqli_close($this->conn);
  185. }
  186. /**
  187. * 直接报错退出
  188. * @param type $msg
  189. */
  190. public function halt($msg = null) {
  191. exit($msg . '<br /><br />' . mysqli_error($this->conn));
  192. }
  193. }