Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $user='gunawan';
- $pass='';
- try {
- $con = new PDO('mysql:host=localhost;dbname=test', $user, $pass); /*buka koneksi*/
- $con->beginTransaction();
- /*=====ADA DATA DI INPUT.. NANTI DIBATALKAN */
- $sql="insert into mytable(name) value('SALAH')";
- $res = $con->exec($sql);
- $insertOK= $res!=false ?$res:false;
- if($insertOK){
- echo "OK .. total perubahan ".$insertOK;
- }
- else{
- throw new PDOException('code:'.$con->errorCode().'|error:'.implode("<br/>",$con->errorInfo()));
- }
- /*QUERY AWAL*/
- echo "<p>DATA AWAL SEBELUM ROLLBACK</p>";
- $sql = "select name from mytable";
- $res = $con->query($sql);
- if(!$res){
- throw new PDOException('code:'.$con->errorCode().'|error:'.implode("<br/>",$con->errorInfo()));
- }
- else{
- foreach ($res as $row) { /*query dan tampilkan*/
- print $row['name'] . "\t";
- print "<br/>";
- }
- }
- /* ROLLBACK */
- $con->rollBack();
- /*QUERY SETELAH ROLLBACK*/
- echo "<p>DATA SESUDAH ROLLBACK</p>";
- $sql = "select name from mytable";
- $res = $con->query($sql);
- if(!$res){
- throw new PDOException('code:'.$con->errorCode().'|error:'.implode("<br/>",$con->errorInfo()));
- }
- else{
- foreach ($res as $row) { /*query dan tampilkan*/
- print $row['name'] . "\t";
- print "<br/>";
- }
- }
- $sql="insert into mytable(name) value('satu'),('dua')";
- $res = $con->exec($sql);
- $insertOK= $res!=false ?$res:false;
- if($insertOK){
- echo "OK .. total perubahan ".$insertOK;
- }
- else{
- throw new PDOException('code:'.$con->errorCode().'|error:'.implode("<br/>",$con->errorInfo()));
- }
- $con = null; /*untuk close */
- } catch (PDOException $e) {
- print "Error!: " . $e->getMessage() . "<br/>";
- die();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement