我正在使用此类来处理我的pdo连接和数据库操作,但是我无法使用回滚功能。我在$ pers-query中创建了一个故意错误,得到以下错误:
( ! ) Warning: PDO::query(): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'webbshop.prson' doesn't exist in C:\wamp\www\webbshop\includes\db_con.php on line 21
但其他2个查询仍然执行,添加到数据库,而不是回滚。我该怎样做才能让一切顺利?
我在另一篇文章中读到你需要使用InnoDB,所以我运行了SHOW ENGINES sql命令,它说对InnoDB的支持是默认的,评论说:“支持事务,行级锁定和外键”
PDO连接类:
<?php
class DB{
private $db_host = "localhost";
private $db_usr = "root";
private $db_pass = "";
private $db_name = "webbshop";
private $db;
function __construct(){
$this->db = new PDO('mysql:host=' . $this->db_host . ';'
.'dbname=' . $this->db_name, $this->db_usr, $this->db_pass);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}
function error(){
} €$€$£
function Trans(){
$this->db->beginTransaction();
}
function insert($sql){
$stmt = $this->db->query($sql);
}
function fetch($sql){
$stmt = $this->db->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
}
function lastInsertID() {
return $this->db->lastInsertId();
}
function commitTrans(){
$this->db->commit();
}
function rollback() {
$this->db->rollBack();
}
function __destruct() {
$this->db = null;
}
}
这是我用来执行查询的代码:
<?php
require 'db_con.php';
try {
$db = new DB();
$db->Trans();
$db->insert("INSERT INTO `webbshop`.`user` (`userID`, `nick`, `pass`) VALUES (NULL, '$_POST[nick]', '$_POST[pass]')");
$nickID = $db->lastInsertID();
echo $nickID;
$pers = "INSERT INTO `webbshop`.`prson` (`personID`, `userID`, `fname`, `lname`, `persnr`, `email`) VALUES (NULL, $nickID, '$_POST[firstname]', '$_POST[lastname]', '$_POST[personnr]','$_POST[email]')";
$addr = "INSERT INTO `webbshop`.`address` (`addressID`, `userID`, `street`, `city`, `zip`) VALUES (NULL, $nickID, '$_POST[address]', '$_POST[city]', '$_POST[zip]')";
$db->insert($pers);
$db->insert($addr);
$db->commitTrans();
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "</br>";
$db->rollback();
}
答案 0 :(得分:2)
您使用的是InnoDB吗?要使用事务,你应该使用InnoDB或回滚不起作用!
编辑:
尝试将PDO :: ERRMODE_WARNING更改为PDO :: ERRMODE_EXCEPTION
答案 1 :(得分:0)
这个问题让我发疯,我想用我的解决方案帮助你。主要问题是我们从另一个班级调用交易。
重要的是使用InnoDB和setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION),最后你的连接必须是开放的单例函数。
我的代码是:
class PDO_db { protected static $ _conn;
- [连接] -
public function __construct(){
if(empty(self::$_conn)){
try{
self::$_conn = new PDO("mysql:host=".DB_SERVER.";dbname=".DB_NAME, DB_USERNAME, DB_PASSWORD, array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES 'UTF8'"));
self::$_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
die("No Connection :: ".DB_NAME." : ".$e->getMessage());
}
}else{
return self::$_conn;
}
}
- [交易] -
public static function Trans(){
return self::$_conn->beginTransaction();
}
- [COMMIT] -
public static function Commit(){
return self::$_conn->commit();
}
- [ROLLBACK] -
public static function Rollback(){
return self::$_conn->rollBack();
}
- [插入] -
public static function Insert($tablo, $rows){
}
}
class db {
public function __construct(){
$this->prodb = new PDO_db();
}
public function datainsert(){
$this->prodb->Trans();
try{
$datainsert = $this->prodb->Insert($table, $data);
$this->prodb->Commit();
$message = "Insert True";
return $message;
}catch(Exception $ex){
$this->prodb->Rollback();
$message = "Insert Flase";
return $message;
}
}
}