1--TEST--
2MySQL PDOStatement->bindValue()
3--EXTENSIONS--
4pdo_mysql
5--SKIPIF--
6<?php
7require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
8MySQLPDOTest::skip();
9$db = MySQLPDOTest::factory();
10?>
11--FILE--
12<?php
13    require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
14    $db = MySQLPDOTest::factory();
15    MySQLPDOTest::createTestTable($db);
16
17    printf("Testing native PS...\n");
18    try {
19        $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0);
20        if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
21            printf("[002] Unable to turn off emulated prepared statements\n");
22
23        printf("Binding variable...\n");
24        $stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? ORDER BY id ASC LIMIT 2');
25        $in = 0;
26        if (!$stmt->bindValue(1, $in))
27            printf("[003] Cannot bind value, %s %s\n",
28                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
29
30        $stmt->execute();
31        $id = $label = null;
32
33        if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
34            printf("[004] Cannot bind integer column, %s %s\n",
35                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
36
37        if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
38            printf("[005] Cannot bind string column, %s %s\n",
39                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
40
41        while ($stmt->fetch(PDO::FETCH_BOUND))
42            printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
43                $in,
44                var_export($id, true), gettype($id),
45                var_export($label, true), gettype($label));
46
47        printf("Binding value and not variable...\n");
48        if (!$stmt->bindValue(1, 0))
49            printf("[006] Cannot bind value, %s %s\n",
50                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
51
52        $stmt->execute();
53        $id = $label = null;
54
55        if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
56            printf("[007] Cannot bind integer column, %s %s\n",
57                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
58
59        if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
60            printf("[008] Cannot bind string column, %s %s\n",
61                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
62
63        while ($stmt->fetch(PDO::FETCH_BOUND))
64            printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
65                $in,
66                var_export($id, true), gettype($id),
67                var_export($label, true), gettype($label));
68
69        printf("Binding variable which references another variable...\n");
70        $in = 0;
71        $in_ref = &$in;
72        if (!$stmt->bindValue(1, $in_ref))
73            printf("[009] Cannot bind value, %s %s\n",
74                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
75
76        $stmt->execute();
77        $id = $label = null;
78
79        if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
80            printf("[010] Cannot bind integer column, %s %s\n",
81                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
82
83        if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
84            printf("[011] Cannot bind string column, %s %s\n",
85                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
86
87        while ($stmt->fetch(PDO::FETCH_BOUND))
88            printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
89                $in,
90                var_export($id, true), gettype($id),
91                var_export($label, true), gettype($label));
92
93
94        printf("Binding a variable and a value...\n");
95        $stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? AND id <= ? ORDER BY id ASC LIMIT 2');
96        $in = 0;
97        if (!$stmt->bindValue(1, $in))
98            printf("[012] Cannot bind value, %s %s\n",
99                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
100
101        if (!$stmt->bindValue(2, 2))
102            printf("[013] Cannot bind value, %s %s\n",
103                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
104
105        $stmt->execute();
106        $id = $label = null;
107
108        if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
109            printf("[014] Cannot bind integer column, %s %s\n",
110                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
111
112        if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
113            printf("[015] Cannot bind string column, %s %s\n",
114                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
115
116        while ($stmt->fetch(PDO::FETCH_BOUND))
117            printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
118                $in,
119                var_export($id, true), gettype($id),
120                var_export($label, true), gettype($label));
121
122        printf("Binding a variable to two placeholders and changing the variable value in between the binds...\n");
123        // variable value change shall have no impact
124        $stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? AND id <= ? ORDER BY id ASC LIMIT 2');
125        $in = 0;
126        if (!$stmt->bindValue(1, $in))
127            printf("[016] Cannot bind value, %s %s\n",
128                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
129
130        $in = 2;
131        if (!$stmt->bindValue(2, $in))
132            printf("[017] Cannot bind value, %s %s\n",
133                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
134
135        $stmt->execute();
136        $id = $label = null;
137
138        if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
139            printf("[018] Cannot bind integer column, %s %s\n",
140                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
141
142        if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
143            printf("[019] Cannot bind string column, %s %s\n",
144                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
145
146        while ($stmt->fetch(PDO::FETCH_BOUND))
147            printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
148                $in,
149                var_export($id, true), gettype($id),
150                var_export($label, true), gettype($label));
151
152    } catch (PDOException $e) {
153        printf("[001] %s [%s] %s\n",
154            $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
155    }
156
157    printf("Testing emulated PS...\n");
158    try {
159        $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 1);
160        if (1 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
161            printf("[002] Unable to turn on emulated prepared statements\n");
162
163        printf("Binding variable...\n");
164        $stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? ORDER BY id ASC LIMIT 2');
165        $in = 0;
166        if (!$stmt->bindValue(1, $in))
167            printf("[003] Cannot bind value, %s %s\n",
168                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
169
170        $stmt->execute();
171        $id = $label = null;
172
173        if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
174            printf("[004] Cannot bind integer column, %s %s\n",
175                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
176
177        if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
178            printf("[005] Cannot bind string column, %s %s\n",
179                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
180
181        while ($stmt->fetch(PDO::FETCH_BOUND))
182            printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
183                $in,
184                var_export($id, true), gettype($id),
185                var_export($label, true), gettype($label));
186
187        printf("Binding value and not variable...\n");
188        if (!$stmt->bindValue(1, 0))
189            printf("[006] Cannot bind value, %s %s\n",
190                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
191
192        $stmt->execute();
193        $id = $label = null;
194
195        if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
196            printf("[007] Cannot bind integer column, %s %s\n",
197                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
198
199        if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
200            printf("[008] Cannot bind string column, %s %s\n",
201                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
202
203        while ($stmt->fetch(PDO::FETCH_BOUND))
204            printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
205                $in,
206                var_export($id, true), gettype($id),
207                var_export($label, true), gettype($label));
208
209        printf("Binding variable which references another variable...\n");
210        $in = 0;
211        $in_ref = &$in;
212        if (!$stmt->bindValue(1, $in_ref))
213            printf("[009] Cannot bind value, %s %s\n",
214                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
215
216        $stmt->execute();
217        $id = $label = null;
218
219        if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
220            printf("[010] Cannot bind integer column, %s %s\n",
221                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
222
223        if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
224            printf("[011] Cannot bind string column, %s %s\n",
225                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
226
227        while ($stmt->fetch(PDO::FETCH_BOUND))
228            printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
229                $in,
230                var_export($id, true), gettype($id),
231                var_export($label, true), gettype($label));
232
233
234        printf("Binding a variable and a value...\n");
235        $stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? AND id <= ? ORDER BY id ASC LIMIT 2');
236        $in = 0;
237        if (!$stmt->bindValue(1, $in))
238            printf("[012] Cannot bind value, %s %s\n",
239                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
240
241        if (!$stmt->bindValue(2, 2))
242            printf("[013] Cannot bind value, %s %s\n",
243                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
244
245        $stmt->execute();
246        $id = $label = null;
247
248        if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
249            printf("[014] Cannot bind integer column, %s %s\n",
250                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
251
252        if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
253            printf("[015] Cannot bind string column, %s %s\n",
254                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
255
256        while ($stmt->fetch(PDO::FETCH_BOUND))
257            printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
258                $in,
259                var_export($id, true), gettype($id),
260                var_export($label, true), gettype($label));
261
262        printf("Binding a variable to two placeholders and changing the variable value in between the binds...\n");
263        // variable value change shall have no impact
264        $stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? AND id <= ? ORDER BY id ASC LIMIT 2');
265        $in = 0;
266        if (!$stmt->bindValue(1, $in))
267            printf("[016] Cannot bind value, %s %s\n",
268                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
269
270        $in = 2;
271        if (!$stmt->bindValue(2, $in))
272            printf("[017] Cannot bind value, %s %s\n",
273                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
274
275        $stmt->execute();
276        $id = $label = null;
277
278        if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
279            printf("[018] Cannot bind integer column, %s %s\n",
280                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
281
282        if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
283            printf("[019] Cannot bind string column, %s %s\n",
284                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
285
286        while ($stmt->fetch(PDO::FETCH_BOUND))
287            printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
288                $in,
289                var_export($id, true), gettype($id),
290                var_export($label, true), gettype($label));
291
292    } catch (PDOException $e) {
293        printf("[001] %s [%s] %s\n",
294            $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
295    }
296
297    print "done!";
298?>
299--CLEAN--
300<?php
301require __DIR__ . '/mysql_pdo_test.inc';
302MySQLPDOTest::dropTestTable();
303?>
304--EXPECT--
305Testing native PS...
306Binding variable...
307in = 0 -> id = 1 (integer) / label = 'a' (string)
308in = 0 -> id = 2 (integer) / label = 'b' (string)
309Binding value and not variable...
310in = 0 -> id = 1 (integer) / label = 'a' (string)
311in = 0 -> id = 2 (integer) / label = 'b' (string)
312Binding variable which references another variable...
313in = 0 -> id = 1 (integer) / label = 'a' (string)
314in = 0 -> id = 2 (integer) / label = 'b' (string)
315Binding a variable and a value...
316in = 0 -> id = 1 (integer) / label = 'a' (string)
317in = 0 -> id = 2 (integer) / label = 'b' (string)
318Binding a variable to two placeholders and changing the variable value in between the binds...
319in = 2 -> id = 1 (integer) / label = 'a' (string)
320in = 2 -> id = 2 (integer) / label = 'b' (string)
321Testing emulated PS...
322Binding variable...
323in = 0 -> id = 1 (integer) / label = 'a' (string)
324in = 0 -> id = 2 (integer) / label = 'b' (string)
325Binding value and not variable...
326in = 0 -> id = 1 (integer) / label = 'a' (string)
327in = 0 -> id = 2 (integer) / label = 'b' (string)
328Binding variable which references another variable...
329in = 0 -> id = 1 (integer) / label = 'a' (string)
330in = 0 -> id = 2 (integer) / label = 'b' (string)
331Binding a variable and a value...
332in = 0 -> id = 1 (integer) / label = 'a' (string)
333in = 0 -> id = 2 (integer) / label = 'b' (string)
334Binding a variable to two placeholders and changing the variable value in between the binds...
335in = 2 -> id = 1 (integer) / label = 'a' (string)
336in = 2 -> id = 2 (integer) / label = 'b' (string)
337done!
338