bug_42499.phpt 2.7 KB

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