我有一个 PDO 类,我创建了它来帮助我与 MySQL 服务器连接和通信。我正在尝试向类添加一个函数或方法,这将帮助我打开一个到 MySQL 的事务以及提交和回滚。我从未使用过提交和回滚。
我认为会以某种方式在每次调用此类时以某种方式停止自动提交,并在抛出错误时回滚并在每次此类 id 关闭时提交。
我的 2 个问题是:1)打开连接然后运行多个查询提交如果没有错误否则回滚然后关闭连接是个好主意吗?2)我如何设法将其添加到我现有的课程中?这是我当前的 PHP 类
<?php
class connection {
private $connString;
private $userName;
private $passCode;
private $server;
private $pdo;
private $errorMessage;
private $pdo_opt = array();
protected $lastQueryTime;
protected $lastQuery;
protected $effectedRows;
function __construct($dbName = DATABASE_NAME, $serverName = DATABASE_HOST){
//SET PDO options
if(TESTING_ENVIRONMENT == 1){
$this->pdo_opt[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$this->pdo_opt[PDO::ATTR_DEFAULT_FETCH_MODE] = PDO::FETCH_ASSOC;
}
$this->pdo_opt[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES utf8';
$this->pdo_opt[PDO::SQLSRV_ATTR_ENCODING] = PDO::SQLSRV_ENCODING_UTF8;
//sets credentials
$this->setConnectionCredentials($dbName, $serverName);
//start the connect
$this->startConnection();
}
function startConnection(){
$this->pdo = new PDO($this->connString, $this->userName, $this->passCode, $this->pdo_opt);
if( ! $this->pdo){
$this->errorMessage = 'Failed to connect to database. Please try to refresh this page in 1 minute. ';
$this->errorMessage .= 'However, if you continue to see this message please contact your system administrator.';
echo $this->getError();
}
}
//this will close the PDO connection
public function endConnection(){
$this->pdo = null;
}
//return a dataset with the results
public function getDataSet($query, $data = NULL)
{
$start = microtime(true);
$cmd = $this->pdo->prepare( $query );
$cmd->execute($data);
$this->effectedRows = $cmd->rowCount();
$ret = $cmd->fetchAll();
//$cmd->closeCursor();
$this->lastQueryTime = microtime(true) - $start;
$this->lastQuery = $query;
return $ret;
}
public function processQuery($query, $data = NULL)
{
$start = microtime(true);
//$this->pdo->beginTransaction();
$cmd = $this->pdo->prepare( $query );
$ret = $cmd->execute($data);
//$this->pdo->commit();
//$cmd->closeCursor();
$this->effectedRows = $cmd->rowCount();
$this->lastQueryTime = microtime(true) - $start;
$this->lastQuery = $query;
return $ret;
}
//return last insert id
public function lastInsertId($name = NULL) {
if(!$this->pdo) {
return false;
}
return $this->pdo->lastInsertId($name);
}
//return last insert id
public function rowCount() {
return $this->effectedRows;
}
public function getOneResult($query, $data = NULL){
$cmd = $this->pdo->prepare( $query );
$cmd->execute($data);
return $cmd->fetchColumn();
}
public function getError(){
if($this->errorMessage != '')
return $this->errorMessage;
else
return true; //no errors found
}
//this where you need to set new server credentials with a new case statment
function setConnectionCredentials($dbName, $serv){
switch($serv){
//the defaults are predefined in the APP_configuration file - DO NOT CHANGE THE DEFAULT
default:
$this->connString = 'mysql:host='.DATABASE_HOST.';dbname='.DATABASE_NAME.';charset=utf8';
$this->userName = DATABASE_USERNAME;
$this->passCode = DATABASE_PASSWORD;
break;
}
}
public function lastQueryTime() {
if(!$this->lastQueryTime) {
throw new Exception('no query has been executed yet');
}
return $this->lastQueryTime;
}
public function lastQuery() {
if(!$this->lastQuery) {
throw new Exception('no query has been executed yet');
}
return $this->lastQuery;
}
}
?>
收集通知后编辑是我需要的这种方式吗?
$db = new connection();
$db->beginTransaction(); //begin transaction
$db->processQuery(); //process query #1
$db->processQuery(); //process query #2
$db->commit(); //commit changes
$db->endConnection(); //close connection
public function beginTransaction(){
$this->pdo->beginTransaction();
}
public function commit(){
$this->pdo->commit();
}
public function rollBack(){
$this->pdo->rollBack();
}
//this will close the PDO connection
public function endConnection(){
$this->pdo = null;
}