CDbUtil.php 6.2 KB

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