1--TEST--
2MySQL PDOStatement->execute()/fetch(), Non-SELECT
3--EXTENSIONS--
4pdo_mysql
5--SKIPIF--
6<?php
7require_once __DIR__ . '/inc/mysql_pdo_test.inc';
8MySQLPDOTest::skip();
9?>
10--FILE--
11<?php
12    require_once __DIR__ . '/inc/mysql_pdo_test.inc';
13    $db = MySQLPDOTest::factory();
14
15    $table = 'pdo_mysql_stmt_fetch_non_select';
16    MySQLPDOTest::createTestTable($table, $db);
17
18    try {
19
20        // Emulated PS first
21        $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 1);
22        if (1 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
23            printf("[002] Unable to turn on emulated prepared statements\n");
24
25        if (!is_object($stmt = $db->query("DESCRIBE {$table} id")))
26            printf("[003] Emulated PS, DESCRIBE failed, %s\n", var_export($db->errorInfo(), true));
27
28        $describe = array();
29        $valid = false;
30        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
31            $describe[] = $row;
32            foreach ($row as $column => $value)
33            if (isset($row['field']) && ($row['field'] == 'id'))
34                $valid = true;
35        }
36        if (empty($describe))
37            printf("[004] Emulated PS, DESCRIBE returned no results\n");
38        else if (!$valid)
39            printf("[005] Emulated PS, DESCRIBE, returned data seems wrong, dumping %s\n",
40                var_export($describe, true));
41
42        if (!is_object($stmt = $db->query('SHOW ENGINES')))
43            printf("[006] Emulated PS, SHOW failed, %s\n", var_export($db->errorInfo(), true));
44
45        $show = array();
46        $valid = false;
47        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
48            $show[] = $row;
49            foreach ($row as $column => $value)
50                // MyISAM engine should be part of _every_ MySQL today
51                if ($value == 'MyISAM')
52                    $valid = true;
53        }
54        if (empty($show))
55            printf("[007] Emulated PS, SHOW returned no results\n");
56        else if (!$valid)
57            printf("[008] Emulated PS, SHOW data seems wrong, dumping %s\n",
58                var_export($show, true));
59
60        if (!is_object($stmt = $db->query("EXPLAIN SELECT id FROM {$table}")))
61            printf("[009] Emulated PS, EXPLAIN returned no results\n");
62
63        $explain = array();
64        while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
65            $explain[] = $row;
66
67        if (empty($explain))
68            printf("[010] Emulated PS, EXPLAIN returned no results\n");
69
70        // And now native PS
71        $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0);
72        if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
73            printf("[011] Unable to turn off emulated prepared statements\n");
74
75        $native_support = 'no';
76        if ($db->exec("PREPARE mystmt FROM 'DESCRIBE {$table} id'")) {
77            $native_support = 'yes';
78            $db->exec('DEALLOCATE PREPARE mystmt');
79        }
80
81        if (!is_object($stmt = $db->query("DESCRIBE {$table} id")))
82            printf("[012] Native PS (native support: %s), DESCRIBE failed, %s\n",
83                $native_support,
84                var_export($db->errorInfo(), true));
85
86        $describe_native = array();
87        $valid = false;
88        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
89            $describe_native[] = $row;
90            foreach ($row as $column => $value)
91            if (isset($row['field']) && ($row['field'] == 'id'))
92                $valid = true;
93        }
94        if (empty($describe_native))
95            printf("[013] Native PS (native support: %s), DESCRIBE returned no results\n",
96                $native_support);
97        else if (!$valid)
98            printf("[014] Native PS (native support: %s), DESCRIBE, returned data seems wrong, dumping %s\n",
99                $native_support,
100                var_export($describe_native, true));
101
102        if ($describe != $describe_native)
103            printf("[015] Emulated and native PS (native support: %s) results of DESCRIBE differ: %s vs. %s\n",
104                $native_support,
105                var_export($describe, true),
106                var_export($describe_native, true));
107
108
109        $native_support = 'no';
110        if ($db->exec("PREPARE mystmt FROM 'SHOW ENGINES'")) {
111            $native_support = 'yes';
112            $db->exec('DEALLOCATE PREPARE mystmt');
113        }
114
115        if (!is_object($stmt = $db->query('SHOW ENGINES')))
116            printf("[016] Native PS (native support: %s), SHOW failed, %s\n",
117                $native_support,
118                var_export($db->errorInfo(), true));
119
120        $show_native = array();
121        $valid = false;
122        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
123            $show_native[] = $row;
124            foreach ($row as $column => $value)
125                // MyISAM engine should be part of _every_ MySQL today
126                if ($value == 'MyISAM')
127                    $valid = true;
128        }
129        if (empty($show_native))
130            printf("[017] Native PS (native support: %s), SHOW returned no results\n",
131                $native_support);
132        else if (!$valid)
133            printf("[018] Native PS (native support: %s), SHOW data seems wrong, dumping %s\n",
134                var_export($show_native, true));
135
136        if ($show != $show_native)
137            printf("Native PS (native support: %s) and emulated PS returned different data for SHOW: %s vs. %s\n",
138                $native_support,
139                var_export($show, true),
140                var_export($show_native, true));
141
142        $native_support = 'no';
143        if ($db->exec("PREPARE mystmt FROM 'EXPLAIN SELECT id FROM {$table}'")) {
144            $native_support = 'yes';
145            $db->exec('DEALLOCATE PREPARE mystmt');
146        }
147
148        if (!is_object($stmt = $db->query("EXPLAIN SELECT id FROM {$table}")))
149            printf("[012] Native PS (native support: %s), EXPLAIN failed, %s\n",
150                $native_support,
151                var_export($db->errorInfo(), true));
152
153        $explain_native = array();
154        while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
155            $explain_native[] = $row;
156
157        if (empty($explain_native))
158            printf("[013] Native PS (native support: %s), EXPLAIN returned no results\n",
159                $native_support);
160
161        if ($explain != $explain_native)
162            printf("Native PS (native support: %s) and emulated PS returned different data for EXPLAIN: %s vs. %s\n",
163                $native_support,
164                var_export($explain, true),
165                var_export($explain_native, true));
166
167        $stmt->execute();
168        $explain_native = $stmt->fetchAll(PDO::FETCH_ASSOC);
169        if ($explain != $explain_native)
170            printf("Native PS (native support: %s) and emulated PS returned different data for EXPLAIN: %s vs. %s\n",
171                $native_support,
172                var_export($explain, true),
173                var_export($explain_native, true));
174
175        $stmt->execute();
176        $stmt->execute();
177        // libmysql needs this - otherwise we get a 2015 error
178        if (!MYSQLPDOTest::isPDOMySQLnd())
179            $stmt->fetchAll(PDO::FETCH_ASSOC);
180
181    } catch (PDOException $e) {
182        printf("[001] %s [%s] %s\n",
183            $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
184    }
185
186    print "done!\n";
187?>
188--CLEAN--
189<?php
190require_once __DIR__ . '/inc/mysql_pdo_test.inc';
191$db = MySQLPDOTest::factory();
192$db->exec('DROP TABLE IF EXISTS pdo_mysql_stmt_fetch_non_select');
193?>
194--EXPECT--
195done!
196