MySQLDB类工具的封装

  面向对象编程的过程中,经常要用到动态数据来填充静态网页,而动态数据往往来自于数据库,为了不用每此创建一个脚本都要重新打一遍连接数据库以及其它的获取资源结果集的代码,因此封装一个数据库的工具类文件就显得尤为重要了。

  数据库工具类的封装,要实现的基本功能要有初始化数据库连接的信息,例如端口、主机号、字符集和数据库名等,然后就是一些常用的功能,例如查询表后获取一个多行多列,单行多列和单行单列的结果集,还有就是一个能用于查询的方法,能够完成查询并返回结果集的功能,并且人为地提示错误信息等,因为数据库采用静默模式,因此默认情况下并不会报错。

  1 <?php
  2 
  3 header("content-type:text/html;charset=utf-8");
  4 //MySQLDB类
  5 class MySQLDB{
  6     private $host;    //主机地址
  7     private $port;    //端口号
  8     private $user;    //用户名
  9     private $pass;    //密码
 10     private $charset;//字符集
 11     private $dbname;    //数据库名
 12     private $link;    //连接资源
 13     private static $instance; //保存对象
 14 
 15     private function __construct($arr){
 16         $this->host=isset($arr['host']) ? $arr['host'] : "localhost";
 17         $this->port=isset($arr['port']) ? $arr['port'] : "3306";
 18         $this->user=isset($arr['user']) ? $arr['user'] : "root";
 19         $this->pass=isset($arr['pass']) ? $arr['pass'] : "";
 20         $this->charset=isset($arr['charset']) ? $arr['charset'] : "utf8";
 21         $this->dbname=isset($arr['dbname']) ? $arr['dbname'] : "";
 22 
 23         $this->my_connect();
 24 
 25         $this->my_charset();
 26 
 27         $this->my_dbname();
 28     }
 29 
 30     public static function getInstance($arr){
 31         if(!self::$instance instanceof self){
 32             self::$instance = new self($arr);
 33         }
 34         return self::$instance;
 35     }
 36 
 37     //连接数据的方法
 38     private function my_connect(){
 39         if($link = @ mysql_connect("$this->host:$this->port",$this->user,$this->pass)){
 40             //连接成功
 41             $this->link = $link;
 42         }else{
 43             //连接失败
 44             echo "连接数据库失败<br/>";
 45             echo "错误代码 ",mysql_errno(), "<br/>";
 46             echo "错误信息 ",mysql_error(), "<br/>";
 47             die;
 48         }
 49     }
 50 
 51     //sql语句执行方法
 52     public function my_query($sql){
 53         $result=mysql_query($sql);
 54         if(!$result){
 55             //执行失败
 56             echo "执行SQL语句失败<br/>";
 57             echo "错误代码 ",mysql_errno(), "<br/>";
 58             echo "错误信息 ",mysql_error(), "<br/>";
 59             echo "错误语句 ",$sql, "<br/>";
 60             die;
 61         }else{
 62             //执行成功
 63             return $result;
 64         }
 65     }
 66 
 67     public function fetchAll($sql){
 68         if($result = $this->my_query($sql)){
 69             //执行成功,遍历结果集
 70             $rows = array();
 71             while($row = mysql_fetch_assoc($result)){
 72                 $rows[] = $row;
 73             }
 74             //结果资源集用完之后最好手动释放
 75             mysql_free_result($result);
 76             //提取成功后返回一个二维数组或空数组
 77             return $rows;
 78         }else{
 79             return false;
 80         }
 81     }
 82 
 83     public function fetchRow($sql){
 84         if($result = $this->my_query($sql)){
 85             //执行成功就返回一个一维数组
 86             $row = mysql_fetch_assoc($result);
 87             //提取结果集后手动释放
 88             mysql_free_result($result);
 89             //返回所有数据或空数组
 90             return $row;
 91         }else{
 92             return flase;
 93         }
 94     }
 95 
 96     public function fetchColumn($sql){
 97         if($result = $this->my_query($sql)){
 98             //执行语句成功,提取结果集
 99             $row = mysql_fetch_row($result);
100             //提取完后主动释放结果集
101             mysql_free_result($result);
102             //返回结果
103             return isset($row[0]) ? $row[0] : flase;
104         }else{
105             return flase;
106         }
107     }
108 
109     //选择默认字符集
110     private function my_charset(){
111         $sql = "set names $this->charset";
112         $this->my_query($sql);
113     }
114 
115     //选择默认数据库
116     private function my_dbname(){
117         $sql = "use $this->dbname";
118         $this->my_query($sql);
119     }
120 
121     public function __destruct(){
122         @ mysql_close($this->link);
123     }
124 
125     public function __sleep(){
126         //序列化数据之前,系统自动调用的方法,可以确定哪些数据需要序列化
127         return array('host','port','user','pass','charset','dbname');
128     }
129 
130     public function __wakeup(){
131         //反序列化的时候,系统自动调用的方法
132         //连接数据库三部曲
133         //连接数据库
134         $this->my_connect();
135         //选择默认字符集
136         $this->my_charset();
137         //选择默认数据库
138         $this->my_dbname();
139     }
140 
141     public function __set($name,$value){
142         $allow_set = array('host','port','user','pass','charset','dbname');
143         if(in_array($name, $allow_set)){
144             $this->$name = $value;
145         }
146     }
147 
148     public function __get($name){
149         $allow_get = array('host','port','charset','dbname');
150         if(in_array($name, $allow_get)){
151             return $this->$name;
152         }else{
153             return flase;
154         }
155     }
156 
157     public function __isset($name){
158         $allow_isset = array('host','port','user','pass','charset','dbname');
159         if(in_array($name, $allow_isset)){
160             return true;
161         }else{
162             return flase;
163         }
164     }
165 
166     public function __unset($name){
167         //unset什么功能都不用实现
168     }
169 
170     private function __clone(){
171 
172     }
173 }

 

转载于:https://www.cnblogs.com/xieyaojin/p/6188762.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值