Advertisement
citstudio

PHP - MYSQL CONTROL TRANSACTION

Jul 29th, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.92 KB | None | 0 0
  1. <?php
  2.     // source code PHP MySQL Control Transaction
  3.     // trans.php
  4.  
  5. /*
  6. DATABASE & TABLE
  7. create database test;
  8. use test;
  9.  
  10. CREATE TABLE trans
  11. (
  12. id int not null auto_increment,
  13. item varchar(30) not null,
  14. quantity varchar(10) not null,
  15. primary key(id)
  16. );
  17.  
  18. INSERT INTO trans (id,item,quantity) VALUES (NULL,'Computer','5');
  19. */
  20.  
  21. function begin() { @mysql_query("BEGIN"); }
  22. function commit() { @mysql_query("COMMIT"); }
  23. function rollback() { @mysql_query("ROLLBACK"); }
  24.  
  25. @mysql_connect("localhost","root", "--ipv6--") or die(mysql_error());
  26. @mysql_select_db("test") or die(mysql_error());
  27.  
  28. $query = "INSERT INTO trans (id,item,quantity) values (null,'Baseball',4)";
  29. begin(); // transaction dimulai
  30. $result = @mysql_query($query);
  31. if(!$result)
  32. {
  33. rollback(); // transaction rolls back
  34. echo "you rolled back";
  35. exit;
  36. }
  37. else
  38. {
  39. commit(); // transaction berhasil di commit
  40. echo "your insertion was successful";
  41. }
  42. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement