class pdoManager
{
private $conn = null;
/**
* 创建数据库链接对象
*/
public function __construct($host,$user,$pwd){
try{
$this->conn = new PDO($host,$user,$pwd);
$this->conn->query('set names utf8');
$this->conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch (PDOException $e){
die('err:'.$e->getMessage());
}
}
/**
* 增删改方法
*/
public function toExec($sql){
try{
$result = $this->conn->exec($sql);
return $result;
}catch (PDOException $e){
die('err:'.$e->getMessage());
}
}
/**
* 数据查询方法
*/
public function getData($sql,$isMore = true){
try{
$result = $this->conn->query($sql);
if($isMore){
$content = $result->fetchAll(PDO::FETCH_ASSOC);
}else{
$content = $result->fetch(PDO::FETCH_ASSOC);
}
return $content;
}catch (PDOException $e){
die('err:'.$e->getMessage());
}
}
/**
* 释放PDO对象
*/
public function __destruct(){
$this->conn = null;
}
}
php封装PDO类
最新推荐文章于 2024-08-29 08:39:22 发布