实用简单的mysql数据库连接类

  1. class DB
  2. {
  3. //database connection
  4. var $con = FALSE;
  5. function DB($MYSQL_HOST=MYSQL_HOST, $MYSQL_USER=MYSQL_USER, $MYSQL_PASS=MYSQL_PASS,$MYSQL_DB=MYSQL_DB)
  6. {
  7. $this->con = @mysql_connect($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASS) or die("Could not connect to database");
  8. if ($this->con)
  9. {
  10. @mysql_select_db($MYSQL_DB, $this->con) or die ("Could not select database");
  11. }
  12. return $this->con;
  13. }
  14. function Query($sql, $tran = false)
  15. {
  16. // if (!file_exists(MYSQL_LOG))
  17. // {
  18. // @umask(0);
  19. // @mkdir(MYSQL_LOG, 0777);
  20. // }
  21. // ¼־
  22. //$fp = @fopen(MYSQL_LOG.date("Ymd").".txt", "a");
  23. // д־
  24. // @fwrite($fp, date("Y-m-d H:i:s")."|$sql ");
  25. // @fclose($fp);
  26. $this->sql = $sql;
  27. if ($tran)
  28. {
  29. $this->result = @mysql_query($this->sql) OR $this->RollBack();
  30. return $this->result;
  31. }
  32. else
  33. {
  34. mysql_query("SET NAMES 'utf8'");
  35. //mysql_query("SET NAMES 'gbk'");
  36. $this->result = @mysql_query($this->sql);
  37. return $this->result;
  38. }
  39. }
  40. function RollBack()
  41. {
  42. $this->Query("ROLLBACK;");
  43. die("MySQL ROLLBACK;");
  44. }
  45. function NumRows($result)
  46. {
  47. $this->result = $result;
  48. return @mysql_num_rows($this->result);
  49. }
  50. function FetchRow($result)
  51. {
  52. $this->result = $result;
  53. return @mysql_fetch_row($this->result);
  54. }
  55. function FetchArray($result)
  56. {
  57. $this->result = $result;
  58. return @mysql_fetch_array($this->result, MYSQL_ASSOC);
  59. }
  60. function FetchArray2($result)
  61. {
  62. $this->result = $result;
  63. return @mysql_fetch_array($this->result, MYSQL_BOTH);
  64. }
  65. function FetchObject($result)
  66. {
  67. $this->result = $result;
  68. return @mysql_fetch_object($this->result);
  69. }
  70. function FreeResult($result)
  71. {
  72. $this->result = $result;
  73. return @mysql_free_result($this->result);
  74. }
  75. function DataSeek($result)
  76. {
  77. //复位记录集指针
  78. $this->result = $result;
  79. return mysql_data_seek($this->result,0);
  80. }
  81. function InsertID()
  82. {
  83. //$this->con = $con;
  84. return @mysql_insert_id($this->con);
  85. }//开源代码phpfensi.com
  86. function Close()
  87. {
  88. if($this->con)
  89. {
  90. @mysql_close($this->con);
  91. }
  92. }
  93. }