VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • php数据库连接类

本文章提供了数据库连接实例,主要是讲php mysql数据相关操作,有需要的朋友可以使用看看,实例代码如下:

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

下面提供一款自动选择数据库远程或本地连接代码:

  1. <?php  
  2. // 包含mysql操作类  
  3. include_once 'mysql.class.php';  
  4. // 本地mysql数据  
  5. $mysql_local_data = array('db_host'=>'localhost',  
  6. 'db_user'=>'root',  
  7. 'db_pass'=>'root',  
  8. 'db_name'=>'test');  
  9. // 远程mysql数据  
  10. $mysql_remote_data = array('db_host'=>'61.183.41.178',  
  11. 'db_user'=>'xxx',  
  12. 'db_pass'=>'xxx',  
  13. 'db_name'=>'xxx');  
  14. // 公用数据  
  15. $tb_prefix = 'php95_';  
  16. $db_charset = 'utf-8';  
  17. //本地连接成功则实例化本地mysql类,否则连接远程数据库并实例化mysql类  
  18. if (@mysql_connect($mysql_local_data[db_host], $mysql_local_data[db_user], $mysql_local_data[db_pass]))  
  19. $db = new mysql($db_host$mysql_local_data[db_user], $mysql_local_data[db_pass], $mysql_local_data[db_name], $db_charset$conn);  
  20. else  
  21. $db = new mysql($mysql_remote_data[db_host], $mysql_remote_data[db_user], $mysql_remote_data[db_pass], $mysql_remote_data[db_name], $db_charset$conn);  
  22. $db->show_tables(); //测试:显示当前数据库下的所有表名  
  23. ?> 

假设我们要在test.php文件中操作虚拟主机的数据库,则首先要在本地调试,那么必然要连接本地、远程两个不同的数据库,问题:怎么让test.php自动识别当下该连接本地还是远程数据库呢?

出处:http://www.phpfensi.com/php/20140911/5360.html
 


相关教程