1--TEST--
2MySQL PDO->prepare(), emulated PS, anonymous placeholder
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
16    try {
17        $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 1);
18        if (1 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
19            printf("[002] Unable to switch to emulated prepared statements, test will fail\n");
20
21        $db->exec('DROP TABLE IF EXISTS test');
22        $db->exec(sprintf('CREATE TABLE test(id INT, label CHAR(255)) ENGINE=%s', PDO_MYSQL_TEST_ENGINE));
23
24        $stmt = $db->prepare("INSERT INTO test(id, label) VALUES(1, '?')");
25        // you can bind as many values as you want no matter if they can be replaced or not
26        $stmt->execute(array('first row'));
27        if ('00000' !== $stmt->errorCode())
28            printf("[003] Execute has failed, %s %s\n",
29                var_export($stmt->errorCode(), true),
30                var_export($stmt->errorInfo(), true));
31
32        $stmt = $db->prepare('SELECT id, label FROM test');
33        $stmt->execute();
34        var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
35
36        // now the same with native PS
37        printf("now the same with native PS\n");
38        $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0);
39        if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
40            printf("[004] Unable to switch off emulated prepared statements, test will fail\n");
41
42        $db->exec('DELETE FROM test');
43        $stmt = $db->prepare("INSERT INTO test(id, label) VALUES(1, '?')");
44        // you can bind as many values as you want no matter if they can be replaced or not
45        $stmt->execute(array('first row'));
46        if ('00000' !== $stmt->errorCode())
47            printf("[005] Execute has failed, %s %s\n",
48                var_export($stmt->errorCode(), true),
49                var_export($stmt->errorInfo(), true));
50
51        $stmt = $db->prepare('SELECT id, label FROM test');
52        $stmt->execute();
53        var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
54
55    } catch (PDOException $e) {
56        printf("[001] %s [%s] %s\n",
57            $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
58    }
59
60    print "done!";
61?>
62--CLEAN--
63<?php
64require __DIR__ . '/mysql_pdo_test.inc';
65$db = MySQLPDOTest::factory();
66$db->exec('DROP TABLE IF EXISTS test');
67?>
68--EXPECTF--
69Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in %s on line %d
70[003] Execute has failed, 'HY093' array (
71  0 => 'HY093',
72  1 => NULL,
73  2 => NULL,
74)
75array(0) {
76}
77now the same with native PS
78
79Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number in %s on line %d
80[005] Execute has failed, 'HY093' array (
81  0 => 'HY093',
82  1 => NULL,
83  2 => NULL,
84)
85array(0) {
86}
87done!
88