php-基于面向对象的MySQL类
时间:2021-07-01 10:21:17
帮助过:2人阅读
sqlHelper{
private $conn;
private $host = ‘localhost‘
;
private $user = ‘root‘
;
private $pwd = ‘root‘
;
private $db;
public function __construct(
$idb){
$this->db =
$idb;
$this->conn =
new mysqli(
$this->host,
$this->user,
$this->
$this->pwd,
$this->
idb);
if(
$this->conn->
connect_error){
$this->error(
$this->conn->
connect_error);
}
$this->conn->query(‘set names utf8‘
);
}
//针对select语句
public function execute_dql(
$sql){
$res =
$this->conn->query(
$sql) or
$this->error(‘查询操作失败‘.
$this->conn->
error);
$this->
free();
return $res;
}
//针对insert、update、delect
public function execute_dml(
$sql){
$flag =
$this->conn->query(
$sql) or
$this->error(
$this->conn->
error);
if(!
$flag){
return 0 ;
//操作失败
}else{
if(
$this->conn->afffected_rows > 0
){
return 1;
//操作成功
}else{
return 2;
//操作失败没有行数受到影响
}
}
}
//针对多行记录
public function fetch_all(
$sql){
$res =
$this->conn->query(
$sql) or
$this->error(‘查询操作失败‘.
$this->conn->
error);
$arr =
array();
while(
$row =
$res->
fetch_assoc()){
$arr[] =
$row;
//及时释放资源
}
return $arr;
}
protected function error(
$err){
$log = ‘cur.log‘
;
file_put_contens($log,
$err,
FILE_APPEND);
die(
$err);
}
}
php-基于面向对象的MySQL类
标签: