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