PHP封装数据库连接
2016-06-14 20:01
387 查看
将数据库连接放在类里面,用的时候直接实例化类。
将数据库地址,用户名,密码做成成员变量
将数据库连接做成成员方法,通过传入的sql语句返回结果集对象
class DBDA{
public $host="localhost";
public $uid="root";
public $pwd = "";
public $dbconnect;
function Query($sql,$type=1,$dbname="z-stu"){
$this->dbconnect = new MySQLi($this->host,$this->uid,$this->pwd,$dbname);
if(!mysqli_connect_error()){
$result = $this->dbconnect->query($sql);
if($type == 1){
$attr = $result->fetch_all();
return $attr;
}else{
return $result;
}
}else{
echo "连接失败";
}
}
}
将数据库地址,用户名,密码做成成员变量
将数据库连接做成成员方法,通过传入的sql语句返回结果集对象
class DBDA{
public $host="localhost";
public $uid="root";
public $pwd = "";
public $dbconnect;
function Query($sql,$type=1,$dbname="z-stu"){
$this->dbconnect = new MySQLi($this->host,$this->uid,$this->pwd,$dbname);
if(!mysqli_connect_error()){
$result = $this->dbconnect->query($sql);
if($type == 1){
$attr = $result->fetch_all();
return $attr;
}else{
return $result;
}
}else{
echo "连接失败";
}
}
}
相关文章推荐
- PHP基础内容
- PHP面向对象基本格式
- PHP合并数组+与array_merge的区别分析
- 全面解读PHP的人气开发框架Laravel
- ftp文件服务器与web项目结合
- php7
- thinkphp分页二,分装到funciton.php
- [PHP实例] php生成圆角图片代码
- [PHP实例] php不破坏单词截取子字符串
- [PHP实例] 一个php 生成zip文件的类
- php多进程实验笔记
- PHP之:PHP框架
- PHP KMP算法实现
- PHP中获取类、方法、属性
- PHP扩展开发小记
- 关于LayoutParams(int width, int height, float weight) 参数解析
- php代码效率测试
- phpmyadmin允许空密码登陆
- 我所认识的PHP(二)
- PHP CI框架中实现事物回滚