###Transaction
function transaction($sqlQueue)
{
//$this->connection();
if(count($sqlQueue)>0)
{
/*
* Manual says:
* If you do not fetch all of the data in a result set before issuing your next call to PDO::query(), your call may fail. Call PDOStatement::closeCursor() to release the database resources associated with the PDOStatement object before issuing your next call to PDO::query().
* */
$this->result->closeCursor();
if($this->charset !== NULL)
{
$this->db->exec("SET NAMES ".$this->charset);
}else{
$this->db->exec("SET NAMES utf8");
}
try
{
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
$this->db->beginTransaction();
foreach ($sqlQueue as $sql)
{
$this->db->exec($sql);
}
$this->db->commit();
return true;
} catch (Exception $e) {
$this->logWriter->writeLog("事务处理出错:".$e->getMessage()."\nSQL语句:".Utils::arrToString($sqlQueue));
$this->db->rollBack();
return false;
}
}else{
return false;
}
}
转自:http://www.flashj.cn/wp/pdo-transaction-err-in-php.html
本文介绍了一种使用 PHP 的 PDO 扩展进行数据库事务处理的方法。通过设置 PDO 属性并利用 try-catch 结构来确保 SQL 语句的正确执行。如果所有 SQL 语句都成功,则提交事务;若出现错误,则回滚并记录错误信息。
1061

被折叠的 条评论
为什么被折叠?



