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