xref: /PHP-8.3/ext/pdo_mysql/tests/bug80458.phpt (revision 72f47c0c)
1--TEST--
2Bug #80458 PDOStatement::fetchAll() throws for upsert queries
3--EXTENSIONS--
4pdo
5pdo_mysql
6--SKIPIF--
7<?php
8require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
9MySQLPDOTest::skip();
10?>
11--FILE--
12<?php
13require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
14
15$db = MySQLPDOTest::factory();
16$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
17$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
18$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
19
20$db->query('DROP TABLE IF EXISTS test');
21$db->query('CREATE TABLE test (first int) ENGINE = InnoDB');
22$res = $db->query('INSERT INTO test(first) VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16)');
23var_dump($res->fetchAll());
24
25$stmt = $db->prepare('DELETE FROM test WHERE first=1');
26$stmt->execute();
27var_dump($stmt->fetchAll());
28
29$res = $db->query('DELETE FROM test WHERE first=2');
30var_dump($res->fetchAll());
31
32$stmt2 = $db->prepare('DELETE FROM test WHERE first=3');
33$stmt2->execute();
34foreach($stmt2 as $row){
35    // expect nothing
36}
37
38$stmt3 = $db->prepare('DELETE FROM test WHERE first=4');
39$stmt3->execute();
40var_dump($stmt3->fetch(PDO::FETCH_ASSOC));
41
42$stmt = $db->prepare('SELECT first FROM test WHERE first=5');
43$stmt->execute();
44var_dump($stmt->fetchAll());
45
46$db->exec('DROP PROCEDURE IF EXISTS nores');
47$db->exec('CREATE PROCEDURE nores() BEGIN DELETE FROM test WHERE first=6; END;');
48$stmt4 = $db->prepare('CALL nores()');
49$stmt4->execute();
50var_dump($stmt4->fetchAll());
51$db->exec('DROP PROCEDURE IF EXISTS nores');
52
53$db->exec('DROP PROCEDURE IF EXISTS ret');
54$db->exec('CREATE PROCEDURE ret() BEGIN SELECT first FROM test WHERE first=7; END;');
55$stmt5 = $db->prepare('CALL ret()');
56$stmt5->execute();
57var_dump($stmt5->fetchAll());
58$stmt5->nextRowset(); // needed to fetch the empty result set of CALL
59var_dump($stmt5->fetchAll());
60$db->exec('DROP PROCEDURE IF EXISTS ret');
61
62/* With emulated prepares */
63print("Emulated prepares\n");
64$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
65
66$stmt = $db->prepare('DELETE FROM test WHERE first=8');
67$stmt->execute();
68var_dump($stmt->fetchAll());
69
70$res = $db->query('DELETE FROM test WHERE first=9');
71var_dump($res->fetchAll());
72
73$stmt2 = $db->prepare('DELETE FROM test WHERE first=10');
74$stmt2->execute();
75foreach($stmt2 as $row){
76    // expect nothing
77}
78
79$stmt3 = $db->prepare('DELETE FROM test WHERE first=11');
80$stmt3->execute();
81var_dump($stmt3->fetch(PDO::FETCH_ASSOC));
82
83$stmt = $db->prepare('SELECT first FROM test WHERE first=12');
84$stmt->execute();
85var_dump($stmt->fetchAll());
86
87$db->exec('DROP PROCEDURE IF EXISTS nores');
88$db->exec('CREATE PROCEDURE nores() BEGIN DELETE FROM test WHERE first=13; END;');
89$stmt4 = $db->prepare('CALL nores()');
90$stmt4->execute();
91var_dump($stmt4->fetchAll());
92$db->exec('DROP PROCEDURE IF EXISTS nores');
93
94$db->exec('DROP PROCEDURE IF EXISTS ret');
95$db->exec('CREATE PROCEDURE ret() BEGIN SELECT first FROM test WHERE first=14; END;');
96$stmt5 = $db->prepare('CALL ret()');
97$stmt5->execute();
98var_dump($stmt5->fetchAll());
99$stmt5->nextRowset(); // needed to fetch the empty result set of CALL
100var_dump($stmt5->fetchAll());
101$db->exec('DROP PROCEDURE IF EXISTS ret');
102
103$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
104$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
105
106$stmt = $db->prepare('DELETE FROM test WHERE first=15');
107$stmt->execute();
108var_dump($stmt->fetchAll());
109
110$stmt = $db->prepare('SELECT first FROM test WHERE first=16');
111$stmt->execute();
112var_dump($stmt->fetchAll());
113
114?>
115--CLEAN--
116<?php
117require __DIR__ . '/mysql_pdo_test.inc';
118MySQLPDOTest::dropTestTable();
119?>
120--EXPECT--
121array(0) {
122}
123array(0) {
124}
125array(0) {
126}
127bool(false)
128array(1) {
129  [0]=>
130  array(2) {
131    ["first"]=>
132    string(1) "5"
133    [0]=>
134    string(1) "5"
135  }
136}
137array(0) {
138}
139array(1) {
140  [0]=>
141  array(2) {
142    ["first"]=>
143    string(1) "7"
144    [0]=>
145    string(1) "7"
146  }
147}
148array(0) {
149}
150Emulated prepares
151array(0) {
152}
153array(0) {
154}
155bool(false)
156array(1) {
157  [0]=>
158  array(2) {
159    ["first"]=>
160    string(2) "12"
161    [0]=>
162    string(2) "12"
163  }
164}
165array(0) {
166}
167array(1) {
168  [0]=>
169  array(2) {
170    ["first"]=>
171    string(2) "14"
172    [0]=>
173    string(2) "14"
174  }
175}
176array(0) {
177}
178array(0) {
179}
180array(1) {
181  [0]=>
182  array(2) {
183    ["first"]=>
184    string(2) "16"
185    [0]=>
186    string(2) "16"
187  }
188}
189