从php7开始mysql扩展库已经被全面移除,原因暂不清楚,官方推荐我们使用mysqli和pdo,这次就针对pdo和mysqli分享下我的两个工具类
1 PDO的mysql操作工具类
这种方式已经用的越来越多了,pdo使用面向对象的方式操作数据库,pdo是很多人都比较推荐的方式。
但需要注意的是: 主机地址最好写成IP地址,例如127.0.0.1而不是localhost,否则容易出现数据库或表 找不到的异常亦或者有的数据库或表可以操作但有的却不行,此情况在版本稍高的mysql上面容易出现
<?php
namespace core;
use \PDO,\PDOStatement,\PDOException;
class PDOUtil{
private $pdo;
private $fetchStyle;
public function __construct($connectInfo=array(),$driver=array()){
$connectInfo["host"]=$connectInfo["host"]??"127.0.0.1";
$connectInfo["port"]=$connectInfo["port"]??"3306";
$connectInfo["dbname"]=$connectInfo["dbname"]??"wxstudy";
$connectInfo["user"]=$connectInfo["user"]??"root";
$connectInfo["password"]=$connectInfo["password"]??"123456";
$charset=$connectInfo["charset"]??"utf8";
$driver[PDO::ATTR_ERRMODE]=$driver[PDO::ATTR_ERRMODE]??PDO::ERRMODE_EXCEPTION;
$this->fetchStyle=$connectInfo["fetchStyle"]??PDO::FETCH_ASSOC;
$dsn="mysql:host=".$connectInfo["host"].";dbname=".$connectInfo["dbname"].";port=".$connectInfo["port"];
try {
$this->pdo=new PDO($dsn,$connectInfo["user"],$connectInfo["password"],$driver);
} catch (PDOException $e) {
echo "数据库连接异常";
echo "错误的文件:".$e->getFile()."</br>";
echo "错误的文件行数:".$e->getLine()."</br>";
echo "异常信息:".$e->getMessage()."</br>";
}
try {
$this->pdo->exec("set names {$charset}");
} catch (PDOException $e) {
$this->printException($e);
}
}
/**增删改通用方法
* @param $sql
* @throws PDOException
* @return number
*/
public function pdo_exec($sql){
try {
$result=$this->pdo->exec($sql);
if($result==0){
throw new PDOException("操作失败,记录未被更改");
}
} catch (PDOException $e) {
$this->printException($e);
}
return $result;
}
/**查询
* @param $sql
* @param boolean $isOnly
* @return mixed|array
*/
public function pdo_query($sql,$isOnly=true){
try {
$stmt=$this->pdo->query($sql);
$stmt->setFetchMode($this->fetchStyle);
if($isOnly){
return $stmt->fetch();
}else{
return $stmt->fetchAll();
}
} catch (PDOException $e) {
$this->printException($e);
}
}
private function printException($e){
echo "SQL执行错误";
echo "错误的文件:".$e->getFile()."</br>";
echo "错误的文件行:".$e->getLine()."</br>";
echo "异常信息:".$e->getMessage()."</br>";
}
}
?>
这里使用构造方法初始化数据的连接、认证、设置字符集编码,从一定程度上也对PDO的异常机制做了处理,使用也比较简单
2 mysqli 工具类
也还不错的一个扩展,提供了面向对象和 面向过程两种方式以供使用,算是中规中矩但也 与时俱进
<?php
class DBUtil{
private $port;
private $userName;
private $password;
private $host;
private $charset;
private $dbName;
public $recordCount;
public $affectedRows;
private $connection;
public function __construct($connectInfo=array()){
$this->port=$connectInfo["port"]??"3306";
$this->userName=$connectInfo["userName"]??"root";
$this->password=$connectInfo["password"]??"123456";
$this->host=$connectInfo["host"]??"localhost";
$this->charset=$connectInfo["charset"]??"utf8";
$this->dbName=$connectInfo["dbName"]??"wxstudy";
$this->connect();
$this->setCharset();
}
private function connect(){
$this->connection=new mysqli($this->host,$this->userName,$this->password,$this->dbName,$this->port);
if( $this->connection->connect_error){
die("数据库连接失败:". $this->connection->connect_error);
}
}
//设置编码
private function setCharset(){
$sql="set names {$this->charset}";
$res=$this->connection->query($sql);
if(!$res){
die("SQL执行出错:". $this->connection->error);
}
}
/** 增删改统一方法
* @param $sql
* @return mysqli_result|boolean
*/
public function exec($sql){
$res=$this->connection->query($sql);
if(!$res){
die("SQL执行出错:". $this->connection->error);
}
$this->affectedRows=$this->connection->affected_rows;
return $res;
}
/**查询
* @param $sql
* @param boolean $isAll
* @return mixed|array
*/
public function query($sql,$isAll=false){
$res=$this->connection->query($sql);
if(!$res){
die("SQL执行出错:". $this->connection->error);
}
$this->recordCount=$res->num_rows;
if($isAll){
//查询多条
return $res->fetch_all(MYSQLI_ASSOC);
}else{
return $res->fetch_assoc();
}
}
/**
* 关闭数据库连接
*/
public function close(){
if($this->connection){
$this->connection->close();
}
}
/**
*获取insert后的主键ID
*/
public function lastInsertId(){
return $this->connection->insert_id;
}
}
?>
如果 使用mysqli扩展建议使用后关闭数据库连接,避免资源浪费