我当前正在重构代码,并收到以下错误:
未捕获的pdoexception:中没有活动事务。。。
class Dbh {
private $serverName;
private $userName;
private $password;
private $dbName;
public function connect(){
$this->serverName = "localhost";
$this->userName = "root";
$this->password = "password";
$this->dbName = "rms";
$this->charset = "utf8";
$dsn = "mysql:host=" . $this->serverName . ";dbname=" . $this->dbName . ";charset=" . $this->charset;
$pdo = new PDO($dsn, $this->userName, $this->password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
}
}
class General extends Dbh {
public function putHistory($consultantID, $type, $typeID) {
$this->connect()->beginTransaction();
$stmt = $this->connect()->prepare("INSERT INTO History (consultantID, type, typeID) VALUES (:consultantID, :type, :typeID) ");
$stmt -> execute(array(':consultantID' => $consultantID, ':type' => $type, ':typeID' => $typeID));
$this->connect()->commit();
}
}
$GeneralObject = new General;
$GeneralObject -> putHistory("1", "2", "3");
我想我是在给警察打电话 beginTransaction()
/ commit()
在这种情况下不正确?
我怎样才能解决这个问题?
1条答案
按热度按时间mwngjboj1#
我想这个错误是由于打电话引起的
$this->connect()
好几次,你打了三次电话putHistory()
. 因此每一次$pdo
返回对象。我建议你打电话来$this->connect()
只执行一次,并将其值存储在变量中,然后调用以下函数:beginTransaction()
,commit()
.