PHP简单封装MysqlHelper类
生活随笔
收集整理的這篇文章主要介紹了
PHP简单封装MysqlHelper类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
MysqlHelper.class.php
1: <?php 2:? 3: /**
4: * Mysql數據幫助類
5: */
6: class MysqlHelper
7: { 8: function __construct()
9: { 10: if(isset($conn)){return;}
11: //創建連接對象
12: $this->conn=@mysql_connect($this->host,$this->uid,$this->pwd); 13: if(!$this->conn){
14: die('連接數據庫失敗:'.mysql_error());
15: } 16:? 17: //選擇數據庫和設置編碼
18: mysql_select_db($this->db,$this->conn); 19: mysql_query('set names utf8');
20: } 21:? 22: private $conn;
23: private $host='localhost';
24: private $uid='root';
25: private $pwd='1234';
26: private $db='test2';
27:? 28: /**
29: * [execute_dql 執行查詢語句]
30: * @param [string] $sql [查詢語句]
31: */
32: public function execute_dql($sql)
33: { 34: $result = mysql_query($sql,$this->conn) or die('執行DQL語句時出錯:'.mysql_error());
35: return $result;
36: } 37:? 38: /**
39: * [execute_dml 執行增刪改語句]
40: * @param [string] $sql [查詢語句]
41: * @return [失敗返回-1,無影響返回0,成功返回受影響的行數]
42: */
43: public function execute_dml($sql)
44: { 45: $result = mysql_query($sql,$this->conn) or die('執行DML語句時出錯:'.mysql_error());
46: if(!$result){
47: return -1; //操作失敗
48: }else{
49: return mysql_affected_rows($this->conn);
50: } 51: } 52:? 53: /**
54: * [show_table_data 顯示表數據]
55: * @param [string] $tableName [表名]
56: * @return [string] [HTML表格]
57: */
58: public function show_table_data($tableName)
59: { 60: $result = $this->execute_dql("select * from $tableName");
61:? 62: $this->show_table($result); 63:? 64: mysql_free_result($result); 65: mysql_close($this->conn); 66: } 67:? 68: /**
69: * [show_table_info 顯示表結構]
70: * @param [string] $tableName [表名]
71: * @return [string] [HTML表格]
72: */
73: public function show_table_info($tableName)
74: { 75: $result = $this->execute_dql("desc $tableName");
76:? 77: $this->show_table($result); 78:? 79: mysql_free_result($result); 80: mysql_close($this->conn); 81: } 82:? 83: /**
84: * [show_table 拼接表格]
85: * @param [resource] $result [結果集]
86: * @return [string] [HTML]
87: */
88: public function show_table($result)
89: { 90: //顯示表頭信息:
91: echo "<br/>數據表:".mysql_field_table($result, 0)." 總行數:".mysql_num_rows($result);
92: $tableData="<table border='1' cellpadding='5'><tr>";
93: $fieldsCount = mysql_num_fields($result); 94: for ($i=0; $i <$fieldsCount; $i++) {
95: $tableData.= "<th>".mysql_field_name($result, $i)."</th>";
96: } 97: $tableData.="</tr>";
98:? 99: //顯示數據信息:
100: while ($row = mysql_fetch_object($result)) {
101: $tableData.="<tr>";
102: foreach ($row as $value) {
103: $tableData.="<td>$value</td>";
104: } 105: $tableData.="</tr>";
106: } 107: $tableData.="</table>";
108:? 109: echo $tableData;
110: } 111: } 112:? 113: ?> 調用示例:
1: <?php 2: header("Content-Type:text/html; charset=utf8");
3:? 4: //自定義MysqlHelper的測試與使用
5: //============================================
6:? 7: //引用自定義的MysqlHelper類
8: require_once "MysqlHelper.class.php";
9:? 10: //實例化MysqlHelper對象
11: $execute = new MysqlHelper();
12:? 13: // 增
14: $sql = "insert into userinfo(uName,uAge,uPwd) values('測試04',18,MD5('1234'));";
15: // 刪
16: // $sql = "delete from userinfo where id=20";
17: // 改
18: // $sql = "update userinfo set uAge=19 where Id=21";
19:? 20: //對數據執行DML操作
21: $returnNum = $execute->execute_dml($sql); 22: if ($returnNum ==-1) {
23: echo "操作失敗 :(";
24: } else {
25: echo "操作成功:) <b style='color:red;'>".$returnNum."</b>行受影響<br/>";
26: } 27:? 28:? 29: //顯示表信息:
30: $execute->show_table_info("userinfo");
31:? 32: //顯示表數據:
33: $execute->show_table_data("userinfo");
34:? 35:? 36: ?>
轉載于:https://www.cnblogs.com/lt-style/p/3511494.html
總結
以上是生活随笔為你收集整理的PHP简单封装MysqlHelper类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 问价,速度 速度 速度
- 下一篇: 微软压力测试工具 web applica