CDbUtil.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace loyalsoft;
  3. // php + mysql fuction class
  4. //error_reporting(E_ALL & ~ E_NOTICE);
  5. /**
  6. * CDB工具类[APHP三大操作单元之一]
  7. * @version 2.0 gwang 升级到mysqli,以便能在PHP7.0以后的环境下继续运行. 约 2017
  8. * 1.0 jgao create 约 2013
  9. * @author jgao,gwang
  10. */
  11. class CDbUtil {
  12. public $conn = 0;
  13. public function dbconn($dbhost, $dbport, $dbuser, $dbpw, $dbname) {
  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. if (!mysqli_select_db($this->conn, $dbname)) {
  31. $this->halt('Cannot use database');
  32. }
  33. }
  34. public function server_info() {
  35. return mysqli_get_server_info($this->conn);
  36. }
  37. public function insert_id() {
  38. $arr = $this->fetch_array('SELECT LAST_INSERT_ID() as id');
  39. return $arr["id"];
  40. }
  41. public function get_value($SQL, $offset = 0, $field = 0) {
  42. $rt = $this->fetch_result($SQL);
  43. if (isset($rt[$offset][$field])) {
  44. return $rt[$offset][$field];
  45. }
  46. return false;
  47. }
  48. /**
  49. * 执行查询
  50. * @param type $SQL
  51. * @param type $method
  52. * @param type $error
  53. * @return mixed
  54. */
  55. public function query($SQL, $method = null, $error = true) {
  56. $query = mysqli_query($this->conn, $SQL);
  57. !$query && $error && $this->halt('Query Error: ' . $SQL);
  58. return $query;
  59. }
  60. /**
  61. * 执行更新的时候加上事物逻辑
  62. * @param type $SQL
  63. * @return int errno
  64. */
  65. public function safeQuery($SQL) {
  66. mysql_query('start transaction');
  67. mysql_query('SET autocommit=0');
  68. $query = mysql_query($SQL, $this->conn);
  69. $err = mysql_errno();
  70. if ($err && !$query) {
  71. mysql_query('rollback');
  72. } else {
  73. mysql_query('commit');
  74. }
  75. mysql_query('SET autocommit=1');
  76. return $err;
  77. }
  78. /**
  79. * 将查询结果作为索引数组返回
  80. * @param string $SQL
  81. * @return array
  82. */
  83. public function fetch_row($SQL) {
  84. return $this->fetch_result($SQL, MYSQLI_NUM);
  85. }
  86. /**
  87. * 将查询结果作为关联数组返回
  88. * @param string $SQL
  89. * @return array
  90. */
  91. public function fetch_array($SQL) {
  92. return $this->fetch_result($SQL, MYSQLI_ASSOC);
  93. }
  94. /**
  95. * 从结果中取一行作为结果返回
  96. * @param string $SQL
  97. * @param int $result_type 1. 关联数组, 2. 索引数组, 3 both
  98. * @return type
  99. */
  100. public function fetch_result($SQL, $result_type = MYSQLI_BOTH) {
  101. $arr = array();
  102. $query = $this->query($SQL);
  103. $data = mysqli_fetch_array($query, $result_type);
  104. while ($data) {
  105. $arr[] = $data;
  106. $data = mysqli_fetch_array($query, $result_type);
  107. }
  108. $this->free_result();
  109. return $arr;
  110. }
  111. public function affected_rows() {
  112. return mysqli_affected_rows($this->conn);
  113. }
  114. /**
  115. * 调取结果集中行的数目
  116. * @param type $SQL
  117. * @return int
  118. */
  119. public function num_rows($SQL) {
  120. $query = $this->query($SQL);
  121. if (!is_bool($query)) {
  122. return mysqli_num_rows($query);
  123. }
  124. return 0;
  125. }
  126. public function num_fields($SQL) {
  127. $query = $this->query($SQL);
  128. return mysqli_num_fields($query);
  129. }
  130. public function escape_string($str) {
  131. return mysqli_real_escape_string($this->conn, $str);
  132. }
  133. public function free_result() {
  134. $void = func_get_args();
  135. foreach ($void as $query) {
  136. if (is_resource($query) && get_resource_type($query) === 'mysql result') {
  137. mysqli_free_result($query);
  138. }
  139. }
  140. unset($void);
  141. }
  142. public function close() {
  143. $this->free_result();
  144. return mysqli_close($this->conn);
  145. }
  146. public function halt($msg = null) {
  147. exit($msg . '<br /><br />' . mysqli_error($this->conn));
  148. }
  149. }