Advertisement
Guest User

php commit bug

a guest
Apr 23rd, 2014
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.22 KB | None | 0 0
  1. <?
  2. # PDO::commit() bug(?) -- cottton @i-stats.net
  3. # db settings:
  4. $user = 'username';
  5. $pw = 'password';
  6. $host = 'localhost';
  7.  
  8.  
  9. # switch here between "bug(?) version" and "working version"
  10. #
  11. $type = 'bug'; # bug(?) version
  12. #$type = 'working'; # how it works
  13.  
  14.  
  15. # 1. connect
  16. #
  17. try{
  18.     $dbh = new PDO('mysql:host='.$host,$user,$pw);
  19. }catch(PDOException $e){
  20.     stop($e->getMessage());
  21. }
  22.  
  23.  
  24. # 2. create database and table for tests
  25. #
  26. foreach(
  27.     array(
  28.         "CREATE SCHEMA IF NOT EXISTS `databasename`;",
  29.         "CREATE TABLE IF NOT EXISTS `databasename`.`testtable`(
  30.            `id` INT unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
  31.            `name` VARCHAR(16) NOT NULL
  32.        );"
  33.     ) as $query
  34. ){
  35.     $stmt = $dbh->prepare($query);
  36.     if(!$stmt->execute()){
  37.         stop($stmt->errorInfo());
  38.     }
  39. }
  40.  
  41.  
  42. # 3. multi insert via beginTransaction() and commit() / rollBack()
  43. #
  44. $dbh->beginTransaction();
  45. $sql = "
  46.    INSERT INTO `databasename`.`testtable` SET name = :name1;
  47.    INSERT INTO `databasename`.`testtable` SET name = :name2;
  48.    INSERT INTO `databasename`.`testtable` SET name = :name3;
  49. ";
  50. $para = array(
  51.     'name1' => 'hans',
  52.     'name2' => 'klaus',
  53.     'name3' => 'olaf'
  54. );
  55. $stmt = $dbh->prepare($sql);
  56. if(!$stmt->execute($para)){
  57.     $dbh->rollBack();
  58.     stop($stmt->errorInfo());
  59. }else{
  60.     switch($type){
  61.         case 'bug':
  62.             echo '<h1>bug(?) "version" (INSERTS wont be done):</h1>';
  63.             test($stmt,'$stmt before $dbh->commit()');
  64.             test($dbh->commit(),'fired $dbh->commit()');
  65.             test($stmt,'$stmt after $dbh->commit()');
  66.             break;
  67.         case 'working':
  68.             echo '<h1>working "version" (INSERTS will be done):</h1>';
  69.             test($stmt,'$stmt before $dbh->commit()');
  70.             test($stmt = 'string','changed $stmt to: $stmt = \'string\'');
  71.             test($dbh->commit(),'fired $dbh->commit()');
  72.             test($stmt,'$stmt after $dbh->commit()');
  73.             break;
  74.         default:
  75.             stop('wrong type! use "bug" or "working"');
  76.             break;
  77.     }
  78. }
  79.  
  80.  
  81. # 4. now disconnect and reconnect to see if the INSERTS really worked:
  82. #
  83. $dbh = NULL;
  84. try{
  85.     $dbh = new PDO('mysql:host=localhost',$user,$pw);
  86. }catch(PDOException $e){
  87.     stop($e->getMessage());
  88. }
  89.  
  90.  
  91. ## 5. read tbl
  92. #
  93. $stmt = $dbh->prepare("SELECT * FROM `databasename`.`testtable`;");
  94. if(!$stmt->execute()){
  95.     stop($stmt->errorInfo());
  96. }else{
  97.     $result = null;
  98.     while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
  99.             $result[] = $row;
  100.     }
  101.     test($result,'SELECT * FROM `databasename`.`testtable`;');
  102. }
  103.  
  104. # thats it
  105.  
  106.  
  107. function stop($e){
  108.     test($e);;
  109.     exit(0);
  110. }
  111. function test($val,$title=''){ # just a dubug func ... life easier ...
  112.    $br = '<br />';
  113.     echo '<pre># <b>DEBUG</b>: <u>'.$title.'</u>'.$br;
  114.     echo '<i>type:</i> '.gettype($val).$br.'<i>val:</i> ';
  115.     if(is_bool($val)){
  116.         echo ($val === true)?'true':'false';
  117.     }elseif(is_null($val)){
  118.         echo 'NULL';
  119.     }elseif(!is_numeric($val)and empty($val)){
  120.         echo 'empty';
  121.     }elseif(is_resource($val)){
  122.         echo get_resource_type($val);
  123.     }else{
  124.         print_r($val);
  125.     }
  126.     echo $br.'#/DEBUG</pre>';
  127. }
  128. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement