mysql数据库连接类

这是一款比较常用的mysql+php的数据库连接类文件代码,这款连接类支持pconnect与mysql_connect来连接,用户可以根据自己的WEB应用类型来处理作选择,mysql数据库连接类代码如下:

  1. $dbhost = 'localhost'; // 数据库服务器
  2. $dbuser = 'root'; // 数据库用户名
  3. $dbpw = 'qwaszx'; // 数据库密码
  4. $dbname = 'movie'; // 数据库名
  5. $adminemail = 'haha@163.com'; // 系统管理员 Email
  6. $database = 'mysql'; // 不能修改此处
  7. $tplrefresh = 1; // 模板自动刷新开关 0=关闭, 1=打开
  8. $pconnect = 0; // 数据库连接方式 0=connect, 1=pconnect
  9. class db_class {
  10. var $querynum = 0;
  11. //function dbstuff() { global $fp; $fp = fopen("./tempdata/dblog.txt", "w"); }
  12. function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0) {
  13. if($pconnect) {
  14. if(!@mysql_pconnect($dbhost, $dbuser, $dbpw)) {
  15. $this->halt('Can not connect to MySQL server');
  16. }
  17. } else {
  18. if(!@mysql_connect($dbhost, $dbuser, $dbpw)) {
  19. $this->halt('Can not connect to MySQL server');
  20. }
  21. }
  22. mysql_query("SET NAMES 'GB2312'");//www.111cn.net
  23. }
  24. function select_db($dbname) {
  25. return mysql_select_db($dbname);
  26. }
  27. function fetch_array($query, $result_type = MYSQL_ASSOC){
  28. $query = mysql_fetch_array($query, $result_type);
  29. return $query;
  30. }
  31. function query($sql, $silence = 0) {
  32. //echo "|$sql|<br>"; //debug
  33. //@fwrite($GLOBALS[fp], $sql." "); //debug
  34. $query = mysql_query($sql);
  35. //echo 'query:'.$query.'<br>';
  36. if(!$query && !$silence)
  37. {
  38. $this->halt('MySQL Query Error', $sql);
  39. }
  40. $this->querynum++;
  41. return $query;
  42. }
  43. function unbuffered_query($sql, $silence = 0) {
  44. $func_unbuffered_query = @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query';
  45. $query = $func_unbuffered_query($sql);
  46. if(!$query && !$silence) {
  47. $this->halt('MySQL Query Error', $sql);
  48. }
  49. $this->querynum++;
  50. return $query;//www.111cn.net
  51. }
  52. function get_one($sql){
  53. $SQL=$sql;
  54. if(function_exists('mysql_unbuffered_query')){
  55. $query = mysql_unbuffered_query($SQL);
  56. }else{
  57. $query = mysql_query($SQL);
  58. }
  59. $this->querynum++;
  60. if (!$query) $this->halt('Query Error: ' . $SQL);
  61. $rs =& mysql_fetch_array($query, MYSQL_ASSOC);
  62. mysql_free_result($query);
  63. return $rs;
  64. }
  65. function affected_rows() {
  66. return mysql_affected_rows();
  67. }
  68. function error() {
  69. return mysql_error();
  70. }
  71. function errno() {
  72. return mysql_errno();
  73. }
  74. function result($query, $row) {
  75. $query = @mysql_result($query, $row);
  76. return $query;www.111cn.net
  77. }
  78. function num_rows($query) {
  79. $query = mysql_num_rows($query);
  80. return $query;
  81. }
  82. function num_fields($query) {
  83. return mysql_num_fields($query);
  84. }
  85. function free_result($query) {
  86. return mysql_free_result($query);
  87. }
  88. function insert_id() {
  89. $id = mysql_insert_id();
  90. return $id;
  91. }
  92. function fetch_row($query) {
  93. $query = mysql_fetch_row($query);
  94. return $query;
  95. }
  96. function close() {
  97. return mysql_close();
  98. }
  99. function halt($message = '', $sql = '') {
  100. echo $message;
  101. }
  102. }
  103. // ========= 连接数据库
  104. $db = new db_class;
  105. $db->connect($dbhost, $dbuser, $dbpw, $pconnect);
  106. //开源代码phpfensi.com
  107. $db->select_db($dbname);
  108. unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);