mysql php连接类

下面这款mysql数据库连接文章,是我从一个cms搞下来的,很完整的,直接调用就OK了,代码如下:

  1. class dbmysq {
  2. var $querynum = 0;
  3. var $link;
  4. var $histories;
  5. var $time;
  6. var $tablepre;
  7. function connect($dbhost, $dbuser, $dbpw, $dbname = '', $dbcharset, $pconnect = 0, $tablepre='', $time = 0) {
  8. $this->time = $time;
  9. $this->tablepre = $tablepre;
  10. if ($pconnect) {
  11. if (!$this->link = mysql_pconnect($dbhost, $dbuser, $dbpw)) {
  12. $this->halt('can not connect to mysql server');
  13. }
  14. } else {
  15. if (!$this->link = mysql_connect($dbhost, $dbuser, $dbpw, 1)) {
  16. $this->halt('can not connect to mysql server');
  17. }
  18. }
  19. if ($this->version() > '4.1') {
  20. if ($dbcharset) {
  21. mysql_query("set character_set_connection=" . $dbcharset . ", character_set_results=" . $dbcharset . ", character_set_client=binary", $this->link);
  22. }
  23. if ($this->version() > '5.0.1') {
  24. mysql_query("set sql_mode=''", $this->link);
  25. }
  26. }
  27. if ($dbname) {
  28. mysql_select_db($dbname, $this->link);
  29. }
  30. }
  31. function fetch_array($query, $result_type = mysql_assoc) {
  32. return mysql_fetch_array($query, $result_type);
  33. }
  34. function result_first($sql, &$data) {
  35. $query = $this->query($sql);
  36. $data = $this->result($query, 0);
  37. }
  38. function fetch_first($sql, &$arr) {
  39. $query = $this->query($sql);
  40. $arr = $this->fetch_array($query);
  41. }
  42. function fetch_all($sql, &$arr) {
  43. $query = $this->query($sql);
  44. while ($data = $this->fetch_array($query)) {
  45. $arr[] = $data;
  46. }
  47. }
  48. function cache_gc() {
  49. $this->query("delete from {$this->tablepre}sqlcaches where expiry<$this->time");
  50. }
  51. function query($sql, $type = '', $cachetime = false) {
  52. $func = $type == 'unbuffered' && @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query';
  53. if (!($query = $func($sql, $this->link)) && $type != 'silent') {
  54. $this->halt('mysql query error', $sql);
  55. }
  56. $this->querynum++;
  57. $this->histories[] = $sql;
  58. return $query;
  59. }
  60. function affected_rows() {
  61. return mysql_affected_rows($this->link);
  62. }
  63. function error() {
  64. return (($this->link) ? mysql_error($this->link) : mysql_error());
  65. }
  66. function errno() {
  67. return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
  68. }
  69. function result($query, $row) {
  70. $query = @mysql_result($query, $row);
  71. return $query;
  72. }
  73. function num_rows($query) {
  74. $query = mysql_num_rows($query);
  75. return $query;
  76. }
  77. function num_fields($query) {
  78. return mysql_num_fields($query);
  79. }
  80. function free_result($query) {
  81. return mysql_free_result($query);
  82. }
  83. function insert_id() {
  84. return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("select last_insert_id()"), 0);
  85. }
  86. function fetch_row($query) {
  87. $query = mysql_fetch_row($query);
  88. return $query;
  89. }
  90. function fetch_fields($query) {
  91. return mysql_fetch_field($query);
  92. }
  93. function version() {
  94. return mysql_get_server_info($this->link);
  95. }
  96. function close() {
  97. return mysql_close($this->link);
  98. }//开源代码phpfensi.com
  99. function halt($message='', $sql='') {
  100. exit('<br/>提示:数据库错误<br/>sql语句:' . $sql . '<br/>错误关键字:' . mysql_error());
  101. }
  102. }