PHP 封装类来访问数据库

封装访问数据库的类

 1 <?php
 2 
 3 class DBDA
 4 {
 5     public $host = "localhost";    //数据库地址
 6     public $uid = "root";       //数据库用户名
 7     public $pwd = "";    //数据库密码
 8     
 9     //执行SQL语句,返回相应的结果的方法
10     //参数:$sql代表要执行的SQL语句,$type是SQL语句类型0代表查询1代表其他,$db代表要操作的数据库
11     public function Query($sql,$type=0,$db="mydb")
12     {
13         //1.造连接对象
14         $dbconnect = new MySQLi($this->host,$this->uid,$this->pwd,$db);
15         //2.判断连接是否出错
16         !mysqli_connect_error() or die("连接失败!");
17         //3.执行SQL语句
18         $result = $dbconnect->query($sql);
19         
20         if($type==0)
21         {
22             return $result->fetch_all();
23         }
24         else
25         {
26             return $result;
27         }
28     }
29 }