国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > PHP > 正文

php數(shù)據(jù)庫(kù)連接類(lèi)

2024-09-04 11:44:15
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文章提供了數(shù)據(jù)庫(kù)連接實(shí)例,主要是講php mysql數(shù)據(jù)相關(guān)操作,有需要的朋友可以使用看看,實(shí)例代碼如下:

  1. <?php  
  2. class mysql {  
  3. private $db_host//主機(jī)地址  
  4. private $db_user//用戶名  
  5. private $db_pass//連接密碼  
  6. private $db_name//名稱(chēng)  
  7. private $db_charset//編碼  
  8. private $conn;  
  9. public $debug=false;//調(diào)試開(kāi)關(guān),默認(rèn)關(guān)閉  
  10. private $query_id//用于判斷sql語(yǔ)句是否執(zhí)行成功  
  11. private $result//結(jié)果集  
  12. private $num_rows//結(jié)果集中行的數(shù)目,僅對(duì)select有效  
  13. private $insert_id//上一步 insert 操作產(chǎn)生的 id  
  14. // 構(gòu)造/析構(gòu)函數(shù)  
  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. // 連接/選擇數(shù)據(jù)庫(kù)  
  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('數(shù)據(jù)庫(kù)-連接失敗:用戶名或密碼錯(cuò)誤!');  
  36. }  
  37. if (!@mysql_select_db($this->db_name,$this->conn)) {  
  38. $this->show_error("數(shù)據(jù)庫(kù)-選擇失敗:數(shù)據(jù)庫(kù) $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語(yǔ)句 <b>"$sql"</b> 執(zhí)行時(shí)遇到錯(cuò)誤");  
  48. return $this->query_id;  
  49. }  
  50. // 顯示詳細(xì)錯(cuò)誤信息  
  51. public function show_error ($msg) {  
  52. if($this->debug){  
  53. $errinfo = mysql_error();  
  54. echo "錯(cuò)誤:$msg <br/> 返回:$errinfo<p>";  
  55. }else{  
  56. echo '<p>出現(xiàn)錯(cuò)誤!<p>';  
  57. }  
  58. }  
  59. // 獲得query執(zhí)行成功與否的信息  
  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. // 顯示共有多少?gòu)埍?nbsp; 
  107. public function show_tables () {  
  108. $this->query("show tables");  
  109. if ($this->query_id) {  
  110. echo "數(shù)據(jù)庫(kù) $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. // 顯示共有多少個(gè)數(shù)據(jù)庫(kù)  
  119. public function show_dbs(){  
  120. $this->query("show databases");  
  121. if ($this->query_id) {  
  122. echo "共有數(shù)據(jù)庫(kù) ".$this->num_rows($this->query_id)." 個(gè)<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. // 刪除數(shù)據(jù)庫(kù):返回刪除結(jié)果  
  131. public function drop_db ($db_name='') {  
  132. if ($db_name == '') {  
  133. $db_name = $this->db_name;//默認(rèn)刪除當(dāng)前數(shù)據(jù)庫(kù)  
  134. $this->query("drop database $db_name");  
  135. }else {  
  136. $this->query("drop database $db_name");  
  137. }  
  138. if ($this->query_id) {  
  139. return "數(shù)據(jù)庫(kù) $db_name 刪除成功";  
  140. }else {  
  141. $this->show_error("數(shù)據(jù)庫(kù) $db_name 刪除失敗");  
  142. }  
  143. }  
  144. // 刪除數(shù)據(jù)表:返回刪除結(jié)果  
  145. public function drop_table ($table_name) {  
  146. $this->query("drop table $table_name");  
  147. if ($this->query_id) {  
  148. return "數(shù)據(jù)表 $table_name 刪除成功";  
  149. }else {  
  150. $this->show_error("數(shù)據(jù)表 $table_name 刪除失敗");  
  151. }  
  152. }  
  153. // 創(chuàng)建數(shù)據(jù)庫(kù)  
  154. public function create_db ($db_name) {  
  155. $this->query("create database $db_name");  
  156. if($this->query_id){  
  157. return "數(shù)據(jù)庫(kù) $db_name 創(chuàng)建成功";  
  158. }else {  
  159. $this->show_error("數(shù)據(jù)庫(kù) $db_name 創(chuàng)建失敗");  
  160. //開(kāi)源代碼Vevb.com 
  161. }  
  162. // 獲取數(shù)據(jù)庫(kù)版本  
  163. public function get_info(){  
  164. echo mysql_get_server_info();  
  165. }  
  166. // 釋放內(nèi)存  
  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. ?>  

下面提供一款自動(dòng)選擇數(shù)據(jù)庫(kù)遠(yuǎn)程或本地連接代碼:

  1. <?php  
  2. // 包含mysql操作類(lèi)  
  3. include_once 'mysql.class.php';  
  4. // 本地mysql數(shù)據(jù)  
  5. $mysql_local_data = array('db_host'=>'localhost',  
  6. 'db_user'=>'root',  
  7. 'db_pass'=>'root',  
  8. 'db_name'=>'test');  
  9. // 遠(yuǎn)程mysql數(shù)據(jù)  
  10. $mysql_remote_data = array('db_host'=>'61.183.41.178',  
  11. 'db_user'=>'xxx',  
  12. 'db_pass'=>'xxx',  
  13. 'db_name'=>'xxx');  
  14. // 公用數(shù)據(jù)  
  15. $tb_prefix = 'php95_';  
  16. $db_charset = 'utf-8';  
  17. //本地連接成功則實(shí)例化本地mysql類(lèi),否則連接遠(yuǎn)程數(shù)據(jù)庫(kù)并實(shí)例化mysql類(lèi)  
  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(); //測(cè)試:顯示當(dāng)前數(shù)據(jù)庫(kù)下的所有表名  
  23. ?> 

假設(shè)我們要在test.php文件中操作虛擬主機(jī)的數(shù)據(jù)庫(kù),則首先要在本地調(diào)試,那么必然要連接本地、遠(yuǎn)程兩個(gè)不同的數(shù)據(jù)庫(kù),問(wèn)題:怎么讓test.php自動(dòng)識(shí)別當(dāng)下該連接本地還是遠(yuǎn)程數(shù)據(jù)庫(kù)呢?

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 霍山县| 宁乡县| 精河县| 平山县| 宁安市| 石屏县| 元氏县| 十堰市| 临夏县| 保定市| 云龙县| 石渠县| 深圳市| 蓬莱市| 正阳县| 阿鲁科尔沁旗| 侯马市| 西贡区| 钦州市| 林甸县| 秦安县| 襄垣县| 邵阳县| 民乐县| 略阳县| 北京市| 绍兴市| 海宁市| 德惠市| 衢州市| 微博| 南部县| 宜都市| 潞城市| 永济市| 蓝田县| 旅游| 通辽市| 常德市| 胶州市| 双峰县|