Fatal error: Uncaught exception ‘PDOException’ with message ‘There is no active transaction’ in C:\xampp\htdocs\VPS\PphCHnew\php\database.php:108 Stack trace: #0 C:\xampp\htdocs\VPS\PphCHnew\php\database.php(108): PDO->rollBack() #1 C:\xampp\htdocs\VPS\PphCHnew\paypal
oPayPal2.php(253): Database->cancelTransaction() #2 C:\xampp\htdocs\VPS\PphCHnew\a4-review-book-confirm.php(2): require_once(‘C:\xampp\htdocs…’) #3 {main} thrown inC:\xampp\htdocs\VPS\PphCHnew\php\database.php on line 108
Whenever I use [with database.php LIBRARY pdo usage from mysql i think] the below I got above error? What must use commit/cancel with database.php library?
mycode.php
try {
$database = new Database();
$database->beginTransaction();
.………….
…………..
$database->endTransaction();
} catch (PDOException $e) {
$database->cancelTransaction();
//echo "Something went wrong: " . $e->getMessage();
} catch (Exception $e) {
$database->cancelTransaction();
//echo "Something went wrong: " . $e->getMessage();
}
database.php
public function cancelTransaction(){
return $this->dbh->rollBack(); // line 108
}
You’ve not used any queries so there is nothing to be rolled back
where ever have
…
…
i have queries
btw this is correct rollback syntax?
can you tell this syntax for all 3 :
database.php pdo lib
plain pdo
plain mysql
Can you post a link to the complete database.php PDO lib? I’ve never seen that one before.
HERE IS-LISTEN IN TUTORIAL - FORGOT WHERE LOCATED
class Database{
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $dbh; //Database Handler
private $error;
private $stmt;
public function __construct(){
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
}
}
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
//Binds the prep statement
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
//Execute the prep statement
public function execute(){
return $this->stmt->execute();
}
//Return result set
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
//Return a single record
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
//Returns number of affected rows
public function rowCount(){
return $this->stmt->rowCount();
}
//Returns last inserted id
public function lastInsertId(){
return $this->dbh->lastInsertId();
}
/* Transaction methods */
public function beginTransaction(){
return $this->dbh->beginTransaction();
}
public function endTransaction(){
return $this->dbh->commit();
}
public function cancelTransaction(){
return $this->dbh->rollBack();
}
//Debug
public function debugDumpParams(){
return $this->stmt->debugDumpParams();
}
}
AS I SEE NOW HAS TRY CATCH … MATTERS IF EXIST TWICE IN CODE?
No because they catch different types of exceptions. If the exception is a PDOException it will be handled by the first block, all other exceptions will be handled by the second block.
That code you posted looks pretty much like I expected it and I don’t think the problem is in that code.
One thing I can think of is that some query goes wrong, terminating the transaction, and then trows an exception. So the transaction doesn’t exist anymore so you can’t cancel it.
Try showing the message of what’s going wrong. In the catch blocks start with echo $e->getMessage()
Another thing is that MyISAM tables don’t support transactions at all, so if you’re using MyISAM that may be what’s tripping it up. Transactions work just fine when using InnoDB tables.