mysql.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. # 文件名称:mysql.php 2009-11-1 16:18:23
  3. # 数据库操作类
  4. class dbmysql {
  5. var $querynum = 0;
  6. var $link;
  7. function dbconn($con_db_host,$con_db_id,$con_db_pass, $con_db_name = '',$db_charset='gb2312',$pconnect = 0) {
  8. if($pconnect) {
  9. if(!$this->link = @mysql_pconnect($con_db_host,$con_db_id,$con_db_pass)) {
  10. $this->halt('Can not connect to MySQL server');
  11. }
  12. } else {
  13. if(!$this->link = @mysql_connect($con_db_host,$con_db_id,$con_db_pass, 1)) {
  14. $this->halt('Can not connect to MySQL server');
  15. }
  16. }
  17. if($this->version() > '4.1') {
  18. if($db_charset!='latin1') {
  19. @mysql_query("SET character_set_connection=$db_charset, character_set_results=$db_charset, character_set_client=binary", $this->link);
  20. }
  21. if($this->version() > '5.0.1') {
  22. @mysql_query("SET sql_mode=''", $this->link);
  23. }
  24. }
  25. if($con_db_name) {
  26. @mysql_select_db($con_db_name, $this->link);
  27. }
  28. }
  29. function move_first($query) {
  30. mysql_data_seek($query,0);
  31. }
  32. function select_db($dbname) {
  33. return mysql_select_db($dbname, $this->link);
  34. }
  35. function fetch_array($query, $result_type = MYSQL_ASSOC) {
  36. if(!$query)
  37. {
  38. return "";
  39. }
  40. else
  41. {
  42. return mysql_fetch_array($query,$result_type);
  43. }
  44. }
  45. function update($table, $bind=array(),$where = '')
  46. {
  47. $set = array();
  48. foreach ($bind as $col => $val) {
  49. $set[] = "$col = '$val'";
  50. unset($set[$col]);
  51. }
  52. $sql = "UPDATE "
  53. . $table
  54. . ' SET ' . implode(',', $set)
  55. . (($where) ? " WHERE $where" : '');
  56. $this->query($sql);
  57. }
  58. function insert($table, $bind=array())
  59. {
  60. $set = array();
  61. foreach ($bind as $col => $val) {
  62. $set[] = "`$col`";
  63. $vals[] = "'$val'";
  64. }
  65. $sql = "INSERT INTO "
  66. . $table
  67. . ' (' . implode(', ', $set).') '
  68. . 'VALUES (' . implode(', ', $vals).')';
  69. $this->query($sql);
  70. return $this->insert_id();
  71. }
  72. /**
  73. * 执行sql语句,只得到一条记录
  74. * @param string sql语句
  75. * @return array
  76. */
  77. function get_one($sql, $type = '')
  78. {
  79. $query = $this->query($sql, $type);
  80. $rs = $this->fetch_array($query);
  81. $this->free_result($query);
  82. return $rs ;
  83. }
  84. function getAll($sql, $type = ''){
  85. $query=array();
  86. $query_search_order=$this->query($sql, $type);
  87. if($query_search_order){
  88. while($row_order=$this->fetch_array($query_search_order)){
  89. $query[]=$row_order;
  90. }
  91. }
  92. if(isset($query)&&count($query)>0){
  93. $this->free_result($query_search_order);
  94. }
  95. return $query;
  96. }
  97. function query($sql, $type = '') {
  98. $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
  99. 'mysql_unbuffered_query' : 'mysql_query';
  100. if(!($query = $func($sql, $this->link))) {
  101. if(in_array($this->errno(), array(2006, 2013)) && substr($type, 0, 5) != 'RETRY') {
  102. $this->close();
  103. global $config_db;
  104. $db_settings = parse_ini_file("$config_db");
  105. @extract($db_settings);
  106. $this->dbconn($con_db_host,$con_db_id,$con_db_pass, $con_db_name = '',$pconnect);
  107. $this->query($sql, 'RETRY'.$type);
  108. }
  109. }
  110. $this->querynum++;
  111. return $query;
  112. }
  113. function counter($table_name,$where_str="", $field_name="*")
  114. {
  115. $where_str = trim($where_str);
  116. if(strtolower(substr($where_str,0,5))!='where' && $where_str) $where_str = "WHERE ".$where_str;
  117. $query = " SELECT COUNT($field_name) FROM $table_name $where_str ";
  118. $result = $this->query($query);
  119. $fetch_row = mysql_fetch_row($result);
  120. return $fetch_row[0];
  121. }
  122. function affected_rows() {
  123. return mysql_affected_rows($this->link);
  124. }
  125. function list_fields($con_db_name,$table) {
  126. $fields=mysql_list_fields($con_db_name,$table,$this->link);
  127. $columns=$this->num_fields($fields);
  128. for ($i = 0; $i < $columns; $i++) {
  129. $tables[]=mysql_field_name($fields, $i);
  130. }
  131. return $tables;
  132. }
  133. function error() {
  134. return (($this->link) ? mysql_error($this->link) : mysql_error());
  135. }
  136. function errno() {
  137. return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
  138. }
  139. function result($query, $row) {
  140. $query = @mysql_result($query, $row);
  141. return $query;
  142. }
  143. function num_rows($query) {
  144. $query = mysql_num_rows($query);
  145. return $query;
  146. }
  147. function num_fields($query) {
  148. return mysql_num_fields($query);
  149. }
  150. function free_result($query) {
  151. return mysql_free_result($query);
  152. }
  153. function insert_id() {
  154. return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
  155. }
  156. function fetch_row($query) {
  157. $query = mysql_fetch_row($query);
  158. return $query;
  159. }
  160. function fetch_fields($query) {
  161. return mysql_fetch_field($query);
  162. }
  163. function version() {
  164. return mysql_get_server_info($this->link);
  165. }
  166. function close() {
  167. return mysql_close($this->link);
  168. }
  169. function halt($message = '',$sql) {
  170. $sqlerror = mysql_error();
  171. $sqlerrno = mysql_errno();
  172. $sqlerror = str_replace($dbhost,'dbhost',$sqlerror);
  173. echo"<html><head><title>CSSInfo</title><style type='text/css'>P,BODY{FONT-FAMILY:tahoma,arial,sans-serif;FONT-SIZE:10px;}A { TEXT-DECORATION: none;}a:hover{ text-decoration: underline;}TD { BORDER-RIGHT: 1px; BORDER-TOP: 0px; FONT-SIZE: 16pt; COLOR: #000000;}</style><body>\n\n";
  174. echo"<table style='TABLE-LAYOUT:fixed;WORD-WRAP: break-word'><tr><td>";
  175. echo"<br><br><b>The URL Is</b>:<br>http://$_SERVER[HTTP_HOST]$REQUEST_URI";
  176. echo"<br><br><b>MySQL Server Error</b>:<br>$sqlerror ( $sqlerrno )";
  177. echo"</td></tr></table>";
  178. exit;
  179. }
  180. }
  181. ?>