Advertisement
Guest User

Untitled

a guest
Oct 18th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. $execute($ddl_partial);
  2. $execute($insert);
  3.  
  4. $execute($ddl_full);
  5. $execute($insert);
  6.  
  7. $execute($drop);
  8. $execute($ddl_partial);
  9. $execute($insert);
  10.  
  11. array(4) {
  12. [0] =>
  13. string(5) "00000"
  14. [1] =>
  15. int(0)
  16. [2] =>
  17. string(24) " ((null)[0] at (null):0)"
  18. [3] =>
  19. string(0) ""
  20. }
  21.  
  22. <?php
  23.  
  24. $hostname = 'microsoftsql.example.com';
  25. $database = 'mydb';
  26. $username = 'user';
  27. $password = 'P@55w0rd';
  28. // https://www.microsoft.com/en-us/download/details.aspx?id=50419
  29. $driver = 'ODBC Driver 13 for SQL Server';
  30.  
  31. $pdo = new PDO("odbc:Driver=$driver;
  32. Server=$hostname;
  33. Database=$database",
  34. $username,
  35. $password
  36. );
  37. $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  38. $pdo->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
  39.  
  40. $drop = "DROP TABLE testerino;";
  41.  
  42. $ddl_full = "
  43. CREATE TABLE testerino (
  44. value VARCHAR(10),
  45. other VARCHAR(10)
  46. );
  47. ";
  48.  
  49. $ddl_partial = "
  50. CREATE TABLE testerino (
  51. value VARCHAR(10)
  52. );
  53. ";
  54.  
  55. $insert = "
  56. INSERT INTO testerino (value)
  57. VALUES ('first');
  58.  
  59. INSERT INTO testerino (value, other)
  60. VALUES ('second', 'another');
  61.  
  62. INSERT INTO testerino (value)
  63. VALUES ('third');
  64. ";
  65.  
  66. $execute = function (String $sql) use ($pdo) {
  67. $pdo->beginTransaction();
  68. try {
  69. $statement = $pdo->prepare($sql);
  70. $statement->execute();
  71. do {
  72. /* https://bugs.php.net/bug.php?id=61613 */
  73. var_dump($statement->errorInfo());
  74. }
  75. while ($statement->nextRowset());
  76. $pdo->commit();
  77. } catch (PDOException $e) {
  78. $pdo->rollBack();
  79. throw $e;
  80. } finally {
  81. $statement->closeCursor();
  82. }
  83. };
  84.  
  85. $execute($drop); // not needed first time
  86. $execute($ddl_full);
  87. $execute($insert);
  88.  
  89. $execute($drop);
  90. $execute($ddl_partial);
  91. $execute($insert);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement