实用mysql数据库连接类

这是一款PHP与mysql数据库连接文件代码,如果你正在找这样功能的代码,可以进来看看,非常完整文件,实例代码如下:

  1. class mysql {
  2. private $db_host; //主机地址
  3. private $db_user; //用户名
  4. private $db_pass; //连接密码
  5. private $db_name; //名称
  6. private $db_charset; //编码
  7. private $conn;
  8. private $query_id; //用于判断sql语句是否执行成功
  9. private $result; //结果集
  10. private $num_rows; //结果集中行的数目,仅对select有效
  11. private $insert_id; //上一步 insert 操作产生的 id
  12. // 构造/析构函数
  13. function __construct ($db_host,$db_user,$db_pass,$db_name,$db_charset,$conn) {
  14. $this->db_host = $db_host ;
  15. $this->db_user = $db_user ;
  16. $this->db_pass = $db_pass ;
  17. $this->db_name = $db_name ;
  18. $this->db_charset = $db_charset ;
  19. $this->conn = $conn ;
  20. $this->connect();
  21. }
  22. function __destruct () {
  23. @mysql_close($this->conn);
  24. }
  25. // 连接/选择数据库
  26. public function connect () {
  27. if ($this->conn == 'pconn') {
  28. @$this->conn = mysql_pconnect($this->db_host,$this->db_user,$this->db_pass);
  29. } else {
  30. @$this->conn = mysql_connect($this->db_host,$this->db_user,$this->db_pass);
  31. }
  32. if (!$this->conn) {
  33. $this->show_error('数据库-连接失败:用户名或密码错误!');
  34. }
  35. if (!@mysql_select_db($this->db_name,$this->conn)) {
  36. $this->show_error("数据库-选择失败:数据库 $this->db_name 不可用");
  37. }
  38. mysql_query("set names $this->db_charset");
  39. return $this->conn;
  40. }
  41. // query方法
  42. public function query ($sql) {
  43. if ($this->query_id) $this->free_result();
  44. $this->query_id = @mysql_query($sql,$this->conn);
  45. if (!$this->query_id) $this->show_error("sql语句 <b>"$sql"</b> 执行时遇到错误");
  46. return $this->query_id;
  47. }
  48. // 查询所有
  49. public function findall ($table_name) {
  50. $this->query("select * from $table_name");
  51. }
  52. // mysql_fetch_array
  53. public function fetch_array () {
  54. if ($this->query_id) {
  55. $this->result = mysql_fetch_array($this->query_id);
  56. return $this->result;
  57. }
  58. }
  59. // ......
  60. public function fetch_assoc () {
  61. if ($this->query_id) {
  62. $this->result = mysql_fetch_assoc($this->query_id);
  63. return $this->result;
  64. }
  65. }
  66. public function fetch_row () {
  67. if ($this->query_id) {
  68. $this->result = mysql_fetch_row($this->query_id);
  69. return $this->result;
  70. }
  71. }
  72. public function fetch_object () {
  73. if ($this->query_id) {
  74. $this->result = mysql_fetch_object($this->query_id);
  75. return $this->result;
  76. }
  77. }
  78. // 获取 num_rows
  79. public function num_rows () {
  80. if ($this->query_id) {
  81. $this->num_rows = mysql_num_rows($this->query_id);
  82. return $this->num_rows;
  83. }
  84. }
  85. // 获取 insert_id
  86. public function insert_id () {
  87. return $this->insert_id = mysql_insert_id();
  88. }
  89. // 显示共有多少张表
  90. public function show_tables () {
  91. $this->query("show tables");
  92. if ($this->query_id) {
  93. echo "数据库 $this->db_name 共有 ".$this->num_rows($this->query_id)." 张表<br/>";
  94. $i = 1;
  95. while ($row = $this->fetch_array($this->query_id)){
  96. echo "$i -- $row[0]<br/>";
  97. $i ++;
  98. }
  99. }
  100. }
  101. // 显示共有多少个数据库
  102. public function show_dbs(){
  103. $this->query("show databases");
  104. if ($this->query_id) {
  105. echo "共有数据库 ".$this->num_rows($this->query_id)." 个<br/>";
  106. $i = 1;
  107. while ($this->row = $this->fetch_array($this->query_id)){
  108. echo "$i -- ".$this->row[database]."<br />";
  109. $i ++;
  110. }
  111. }
  112. }
  113. // 删除数据库:返回删除结果
  114. public function drop_db ($db_name='') {
  115. if ($db_name == '') {
  116. $db_name = $this->db_name;//默认删除当前数据库
  117. $this->query("drop database $db_name");
  118. }else {
  119. $this->query("drop database $db_name");
  120. }
  121. if ($this->query_id) {
  122. return "数据库 $db_name 删除成功";
  123. }else {
  124. $this->show_error("数据库 $db_name 删除失败");
  125. }
  126. }
  127. // 删除数据表:返回删除结果
  128. public function drop_table ($table_name) {
  129. $this->query("drop table $table_name");
  130. if ($this->query_id) {
  131. return "数据表 $table_name 删除成功";
  132. }else {
  133. $this->show_error("数据表 $table_name 删除失败");
  134. }
  135. }
  136. // 创建数据库
  137. public function create_db ($db_name) {
  138. $this->query("create database $db_name");
  139. if($this->query_id){
  140. return "数据库 $db_name 创建成功";
  141. }else {
  142. $this->show_error("数据库 $db_name 创建失败");
  143. }
  144. }
  145. // 获取数据库版本
  146. public function get_info(){
  147. echo mysql_get_server_info();
  148. }
  149. // 显示错误信息
  150. public function show_error ($msg) {
  151. $errinfo = mysql_error();
  152. echo "错误:$msg <br/> 返回:$errinfo<p>";
  153. }//开源代码phpfensi.com
  154. // 释放内存
  155. public function free_result () {
  156. if ( @mysql_free_result($this->query_id) )
  157. unset ($this->result);
  158. $this->query_id = 0;
  159. }
  160. } // end class