php连接mySql数据库 示例

  1 <?php
  2 header("content-type:text/html;charset=utf-8");
  3 error_reporting(E_ALL);
  4 
  5 /*
  6 连接数据库---类被实例化的自动连接
  7 获取提示信息的方法
  8 查询的方法
  9     单条查询
 10     多条查询
 11 增删改的方法
 12 统计记录数的方法
 13 关闭数据库、销毁资源----程序结束的时候调用
 14  */
 15 class dbManage{
 16     private $conn;  #保存数据库连接
 17     public $mess;  #保存提示信息
 18     /*
 19     方法名:__construct   构造方法
 20     方法功能: 类实例化的同时连接数据库
 21     方法参数:$hostname    主机地址
 22               $username   数据库用户名
 23               $password   数据库密码
 24               $dbname   数据库名称
 25               
 26      */
 27     public function __construct($hostname,$username,$password,$dbname){
 28         $this->conn=mysqli_connect($hostname,$username,$password,$dbname); #连接数据库
 29         //判断数据库是否正确连接
 30         if($this->conn){
 31             $this->getMessage('连接成功',true);
 32             $res=mysqli_query($this->conn,"set names utf8");
 33             //判断编码是否正确设置
 34             if($res){
 35                 $this->getMessage('数据库编码设置为utf8',true);
 36             }else{
 37                 $this->getMessage(mysqli_errno($this->conn).":".mysqli_error($this->conn),false);
 38             }
 39         }else{
 40             $this->getMessage('连接失败',false);
 41         }
 42     }
 43     /*
 44     方法名:getMessage   
 45     方法功能: 改造提示信息
 46     方法参数:$info   提示信息
 47               $flag    true表示正确信息绿色文字   false  表示失败信息红色文字 
 48      */
 49     public function getMessage($info,$flag){
 50         if($flag){
 51 
 52             $this->mess.="<div style='color:green;'>".$info."</div>";
 53         }else{
 54             $this->mess.="<div style='color:red;'>".$info."</div>";
 55         }
 56     }
 57     /*
 58     方法名: getMoreData  
 59     方法功能: 获取多条记录
 60     方法参数:$sql  sql语句
 61      */
 62     public function getMoreData($sql){
 63         $rreg=preg_match('/^select/',trim($sql));#验证$sql是否为查询语句
 64 
 65         if($rreg){
 66             $data=mysqli_query($this->conn,$sql);
 67             //判断mysqli_query是否执行成功
 68             if($data){
 69             //将资源类型的数据$data改变成数组
 70             while($arr=mysqli_fetch_array($data,MYSQL_ASSOC)){
 71                 $res[]=$arr;
 72             }
 73             return $res;  #将执行成功结果返回
 74         }else{
 75             $this->getMessage(mysqli_errno($this->conn).":".mysqli_error($this->conn),false);
 76         }
 77         }else{
 78             $this->getMessage(__FUNCTION__.'()只能用于查询多条记录',false);
 79         }
 80         
 81     }
 82     /*
 83     方法名: getOneData  
 84     方法功能: 获取单条记录
 85     方法参数:$sql  sql语句
 86      */
 87     public function getOneData($sql){
 88         $rreg=preg_match('/^select/',trim($sql));
 89         if($rreg){
 90             $data=mysqli_query($this->conn,$sql);
 91             if($data){
 92             $arr=mysqli_fetch_array($data,MYSQL_ASSOC);
 93             return $arr;
 94         }else{
 95             $this->getMessage(mysqli_errno($this->conn).":".mysqli_error($this->conn),false);
 96         }
 97         }else{
 98             $this->getMessage(__FUNCTION__.'()只能用于查询单条记录',false);
 99         }
100         
101     }
102     /*
103     方法名: execSql  
104     方法功能: 用于执行insert|update|delete|drop语句
105     方法参数:$sql  sql语句
106      */
107     public function execSql($sql){
108         $rreg=preg_match('/^insert|delete|update|drop/',trim($sql));
109         if($rreg){
110             $data=mysqli_query($this->conn,$sql);
111             if($data){
112                 $this->getMessage($sql.'语句执行成功',true);
113                 return true;
114             }else{
115                 $this->getMessage(mysqli_errno($this->conn).":".mysqli_error($this->conn),false);
116                 return false;
117             }
118         }else{
119             $this->getMessage(__FUNCTION__.'()只能用于执行insert|update|delete|drop语句',false);
120             return false;
121         }
122     }
123     /*
124     方法名: getRows 
125     方法功能: 获取满足条件的记录数
126     方法参数:$tablename   表名称
127               $tj   条件,默认为1,1表示统计整张表的记录数
128      */
129     public function getRows($tablename,$tj=1){
130         // select count(*) from student where cj>80;
131         $sql="select count(*) as c from ".$tablename." where ".$tj;
132         $data=mysqli_query($this->conn,$sql);
133         if($data){
134             $arr=mysqli_fetch_array($data,MYSQL_ASSOC);
135             // print_r($arr['c']);
136             return $arr['c'];
137         }else{
138             $this->getMessage(mysql_errno($this->conn).":".mysqli_error($this->conn),false);
139         }
140     }
141     /*
142     方法名: __destruct   析构方法
143     方法功能:销毁资源,关闭数据库   程序结束自动调用
144     方法参数:无
145      */
146     public function __destruct(){
147         mysqli_close($this->conn); #关闭数据库
148         unset($this->mess);#销毁mess
149     }
150 }
151 
152 ?>