puiu91
3/15/2016 - 1:36 PM

PHP Database PDO with rollback

PHP Database PDO with rollback

<?php

$this->database->beginTransaction();
$statement = $this->database->prepare($query);
$statement->execute([
  ':beginning' => $beginning,
  ':end'       => $end,
  ':codes'     => $codes,
]);

<?php

try {
    $statement = $MySQL->prepare('SELECT * FROM someTable WHERE id = ?');
    $statement->bindValue(1, $someVar, \PDO::PARAM_INT);
    $statement->execute();
    $databaseData = $statement->fetch();
} catch (PDOException $e) {
    echo $e->getMessage();
}
<?php

try {
    
    $MySQL->beginTransaction();
    
    $statement = $MySQL->prepare('INSERT INTO vehicles(make, model) VALUES(?,?)');
    $statement->bindValue(1, $make, \PDO::PARAM_INT);
    $statement->bindValue(2, $model, \PDO::PARAM_STR);
    $statement->execute();
    
    $MySQL->commit();
    
} catch (PDOException $e) {
    
    $MySQL->rollBack();
    echo $e->getMessage();
    
}

<?php
        
try {

    $db = new App\Database\DatabaseHandler($credentials);
    $MySQL = $db->getInstance('MySQL');

    $MySQL->beginTransaction();

    $statement = $MySQL->prepare($query);
    $statement->execute($array);

    $MySQL->commit();

} catch(PDOException $e) {
        
    $MySQL->rollBack();
    echo $e->getMessage();
    
}