Guest User

php commit bug

a guest
Apr 24th, 2014
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.07 KB | None | 0 0
  1. <?
  2. /* contact: cottton (@i-stats.net or ts3:i-stats.net)
  3. EXPLANATION:
  4.     using PDO to fire more than 1 "INSERT INTO" via PDO::beginTransaction() and PDO::commit()
  5.     Q: what does it mean?
  6.     A: this means i try to send more than 1 query to the mysql server at once:
  7.  
  8.     $dbh->beginTransaction();
  9.     $sql = "
  10.        INSERT INTO `testdb`.`tbl` SET `name` = 'a';
  11.        INSERT INTO `testdb`.`tbl` SET `name` = 'b';
  12.        INSERT INTO `testdb`.`tbl` SET `name` = 'c';
  13.     ";
  14.     $stmt = $dbh->prepare($sql);
  15.     $stmt->execute();
  16.     $dbh->commit();
  17.  
  18.     Q: does this work?
  19.     A: no. somehow the mysql server still waits for the commit.
  20.  
  21.  
  22. THE TEST SCRIPT:
  23.     Q: is it 10 to 20 lines?
  24.     A: no. hell! i cant write you that test script in 20 lines.
  25.  
  26.     explanation:
  27.     there are 2 ways we will fire this multi-query:
  28.     1. regular as above
  29.     2. a alternative way: we "destroy" the $stmt before we fire the ->commit()
  30.  
  31.     Q: how do i use this test script?
  32.     A: just adapt the settings:
  33.         $mysql_username, $mysql_password, $mysql_host and the $type
  34.  
  35.     Q: for what is the $type for?
  36.     A: it allows you to switch between the 2 ways we use to fire the multi-query.
  37.         so you will first look at the regular way to see that the multi-inserts didnt work,
  38.         and then switch to the alternative way to see that they surprisingly worked.
  39.     (!) please be sure you truncated the table after using the alternative way
  40.         i wont do that for you ...
  41.  
  42.     Q: how do i see the result?
  43.     A: in your browser =)
  44.  
  45. you dont really have to read the script.
  46. the output shows you what happen.
  47. so here is the script:
  48. (and this is the last time ill try to explain you that bug, sorry but ...)
  49. */
  50.  
  51. # db settings:
  52. $mysql_username = 'username';
  53. $mysql_password = 'password';
  54. $mysql_host     = 'localhost';
  55.  
  56. # switch here the $type (un-/comment one of those):
  57. $type = 'regular'; # regular way
  58. #$type = 'alternative'; # alternative way
  59.  
  60.  
  61. echo '<h1>using PDO to insert multi-querys using beginTransaction() and commit()</h1>';
  62. # 1. connect
  63. try{
  64.     $dbh = new PDO('mysql:host='.$mysql_host,$mysql_username,$mysql_password);
  65. }catch(PDOException $e){
  66.     stop($e->getMessage());
  67. }
  68. echo 'connected to mysql server<br>';
  69.  
  70. # 2. create database and table for tests
  71. foreach(
  72.     array(
  73.         "CREATE SCHEMA IF NOT EXISTS `testdb`;",
  74.         "CREATE TABLE IF NOT EXISTS `testdb`.`tbl`(
  75.           `id` INT unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
  76.           `name` VARCHAR(16) NOT NULL
  77.       );"
  78.     ) as $query
  79. ){
  80.     $stmt = $dbh->prepare($query);
  81.     if(!$stmt->execute()){
  82.         stop($stmt->errorInfo());
  83.     }
  84. }
  85. echo 'created database and table if not already exists<br>';
  86.  
  87. # 3. multi insert via beginTransaction() and commit() / rollBack()
  88. $dbh->beginTransaction();
  89. echo '<b>$dbh->beginTransaction();</b><br>';
  90. $sql = "
  91.   INSERT INTO `testdb`.`tbl` SET `name` = 'a';
  92.   INSERT INTO `testdb`.`tbl` SET `name` = 'b';
  93.   INSERT INTO `testdb`.`tbl` SET `name` = 'c';
  94. ";
  95. echo '<b>INSERT INTO `testdb`.`tbl` SET `name` = \'a\';</b><br><b>INSERT INTO `testdb`.`tbl` SET `name` = \'b\';</b><br><b>INSERT INTO `testdb`.`tbl` SET `name` = \'c\';</b><br>';
  96. $stmt = $dbh->prepare($sql);
  97. echo '<b>$stmt = $dbh->prepare($sql);</b><br>';
  98. if(!$stmt->execute()){
  99.     $dbh->rollBack();
  100.     stop($stmt->errorInfo());
  101. }else{
  102.     ############################
  103.    # INTERESTING PART IS HERE #
  104.    ############################
  105.    echo '<br><i>we will now ->commit()</i><br>';
  106.     switch($type){
  107.         case 'regular':
  108.             echo '<h2>using regular way to see that the multi-inserts didnt work</h2>';
  109.             $dbh->commit();
  110.             echo 'fired <b>$dbh->commit();</b><br>';
  111.             break;
  112.         case 'alternative':
  113.             echo '<h2>using the alternative way to see that they surprisingly worked</h2>';
  114.             $stmt = null;
  115.             echo '"destroyed" the $stmt: <b>$stmt = null;</b><br>';
  116.             $dbh->commit();
  117.             echo 'and now fired <b>$dbh->commit();</b><br>';
  118.             break;
  119.         default:
  120.             stop('wrong type! use "regular" or "alternative"');
  121.             break;
  122.     }
  123. }
  124.  
  125. # 4. now (have to) disconnect and reconnect to see if the INSERTS really worked:
  126. # (you may try your self without reconnecting...)
  127. $dbh = NULL;
  128. try{
  129.     $dbh = new PDO('mysql:host='.$mysql_host,$mysql_username,$mysql_password);
  130. }catch(PDOException $e){
  131.     stop($e->getMessage());
  132. }
  133. echo 'reconnected to mysql server<br>';
  134.  
  135. ## 5. read tbl
  136. echo 'read tbl: <b>SELECT * FROM `testdb`.`tbl`;</b><br>';
  137. $stmt = $dbh->prepare("SELECT * FROM `testdb`.`tbl`;");
  138. if(!$stmt->execute()){
  139.     stop($stmt->errorInfo());
  140. }else{
  141.     while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
  142.         $res[] = $row;
  143.     }
  144.     echo '<b>result:</b><br>';
  145.     if(!isset($res)){
  146.         echo 'we got no data back from the mysql server<br>';
  147.     }else{
  148.         echo '<pre>';
  149.         print_r($res);
  150.         echo '</pre>';
  151.     }
  152. }
  153.  
  154. # e-handling
  155. function stop($e){
  156.     echo '<pre>';
  157.     print_r($e);
  158.     echo '</pre>';
  159.     exit(0);
  160. }
  161.  
  162. ?>
Advertisement
Add Comment
Please, Sign In to add comment