bug_42499.phpt 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. --TEST--
  2. Bug #42499 (Multi-statement execution via PDO::exec() makes connection unusable)
  3. --SKIPIF--
  4. <?php
  5. if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) die('skip not loaded');
  6. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'skipif.inc');
  7. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
  8. MySQLPDOTest::skip();
  9. $db = MySQLPDOTest::factory();
  10. $stmt = $db->query('SELECT VERSION() as _version');
  11. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  12. $matches = array();
  13. if (!preg_match('/^(\d+)\.(\d+)\.(\d+)/ismU', $row['_version'], $matches))
  14. die(sprintf("skip Cannot determine MySQL Server version\n"));
  15. $version = $matches[0] * 10000 + $matches[1] * 100 + $matches[2];
  16. if ($version < 41000)
  17. die(sprintf("skip Need MySQL Server 4.1.0+, found %d.%02d.%02d (%d)\n",
  18. $matches[0], $matches[1], $matches[2], $version));
  19. --FILE--
  20. <?php
  21. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
  22. $db = MySQLPDOTest::factory();
  23. function bug_42499($db) {
  24. $db->exec('DROP TABLE IF EXISTS test');
  25. $db->exec("CREATE TABLE test(id CHAR(1)); INSERT INTO test(id) VALUES ('a')");
  26. $stmt = $db->query('SELECT id AS _id FROM test');
  27. var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
  28. // You must not use exec() to run statements that create a result set!
  29. $db->exec('SELECT id FROM test');
  30. // This will bail at you because you have not fetched the SELECT results: this is not a bug!
  31. $db->exec("INSERT INTO test(id) VALUES ('b')");
  32. }
  33. print "Emulated Prepared Statements...\n";
  34. $db = MySQLPDOTest::factory();
  35. $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
  36. $db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, 1);
  37. bug_42499($db);
  38. print "Native Prepared Statements...\n";
  39. $db = MySQLPDOTest::factory();
  40. $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
  41. $db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, 1);
  42. bug_42499($db);
  43. $db = MySQLPDOTest::factory();
  44. $db->exec('DROP TABLE IF EXISTS test');
  45. print "done!";
  46. ?>
  47. --EXPECTF--
  48. Emulated Prepared Statements...
  49. array(1) {
  50. [0]=>
  51. array(1) {
  52. [%u|b%"_id"]=>
  53. %unicode|string%(1) "a"
  54. }
  55. }
  56. Warning: PDO::exec(): SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute. in %s on line %d
  57. Native Prepared Statements...
  58. array(1) {
  59. [0]=>
  60. array(1) {
  61. [%u|b%"_id"]=>
  62. %unicode|string%(1) "a"
  63. }
  64. }
  65. Warning: PDO::exec(): SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute. in %s on line %d
  66. done!