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