Advertisement
rfv123

questions/32690361 - interesting error handling

Sep 21st, 2015
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. =====================
  2. Valid SQL statements:
  3. ---------------------
  4.  
  5. $sth = $conn->prepare('LOCK TABLE aaa WRITE;'
  6. .'INSERT INTO aaa (`amount`, `other_column`) VALUES (:amount, :other);'
  7. .'UNLOCK TABLE aaa;'
  8. );
  9.  
  10. $allOK = $sth->execute(array(':amount' => 42.34, ':other' => 'soq'));
  11. var_dump('lastInsertId', $conn->lastInsertId(), 'Execute Succeeded?', $allOK);
  12.  
  13. What is sent to the mysql server:
  14. .................................
  15.  
  16. 150921 10:54:58 55 Connect test@localhost on testmysql
  17. 55 Query LOCK TABLE aaa WRITE;INSERT INTO aaa (`amount`, `other_column`) VALUES ('42.340000000000003', 'soq');UNLOCK TABLE aaa
  18. 54 Query FLUSH LOGS
  19.  
  20. Execute Result from PDO:
  21. .........................
  22.  
  23. 'lastInsertId': '0' (length=1)
  24. 'Execute Succeeded?', boolean true
  25.  
  26. Notice - you do not get the 'LastInsertId'.
  27.  
  28. Database result:
  29. ...........................
  30.  
  31. 1 record inserted:
  32.  
  33. It all worked fine - except you do not know the actual `id` - but you can find it out.
  34.  
  35. ============================================
  36. VERY Invalid SQL statements after the first:
  37. --------------------------------------------
  38.  
  39. $sth = $conn->prepare('LOCK TABLE aaa WRITE;'
  40. .'INSERT INTO CRAPaaa (`CRAPamount`, `CRAPother_column`) VALUES (:amount, :other);'
  41. .'UNLOCK TABLE CRAPaaa;'
  42. );
  43.  
  44. $allOK = $sth->execute(array(':amount' => 42.34, ':other' => 'soq'));
  45. var_dump('lastInsertId', $conn->lastInsertId(), 'Execute Succeeded?', $allOK);
  46.  
  47. What is sent to the mysql server:
  48. .................................
  49.  
  50. 63 Connect test@localhost on testmysql
  51. 63 Query LOCK TABLE aaa WRITE;INSERT INTO CRAPaaa (`CRAPamount`, `CRAPother_column`) VALUES ('42.340000000000003', 'soq');UNLOCK TABLE CRAPaaa
  52. 62 Query FLUSH LOGS
  53.  
  54.  
  55. Execute Result from PDO:
  56. .........................
  57.  
  58. 'lastInsertId', '0'
  59. 'Execute Succeeded?', boolean true
  60.  
  61. It failed! And there is no error indicated at all!
  62.  
  63. Oh dear... ;-/
  64.  
  65. // ----------------------------------------------------------------------------
  66.  
  67. =====================
  68. PDO Connection setup:
  69. ---------------------
  70.  
  71. $pdo_connectionstring = 'mysql:host=localhost;dbname=testmysql';
  72. $pdo_username = 'test';
  73. $pdo_password = 'test';
  74.  
  75. $conn = new PDO($pdo_connectionstring, $pdo_username, $pdo_password);
  76. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  77. $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement