1--TEST--
2MySQL PDO->prepare(), native PS, named 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
18        $db->exec('DROP TABLE IF EXISTS test');
19        $db->exec(sprintf('CREATE TABLE test(id INT, label CHAR(255)) ENGINE=%s', PDO_MYSQL_TEST_ENGINE));
20
21        $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0);
22        if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
23            printf("[002] Unable to turn off emulated prepared statements\n");
24
25        // INSERT a single row
26        $stmt = $db->prepare("INSERT INTO test(id, label) VALUES (100, ':placeholder')");
27
28        // Yes, there is no placeholder to bind to and named placeholder
29        // do not work with MySQL native PS, but lets see what happens!
30        // The ':placeholder' is a string constant in the INSERT statement.
31        // I would expect to get an error message, but this is not what happens.
32        $stmt->execute(array(':placeholder' => 'row1'));
33        if ('00000' !== $stmt->errorCode())
34            printf("[003] Execute has failed, %s %s\n",
35                var_export($stmt->errorCode(), true),
36                var_export($stmt->errorInfo(), true));
37
38        // Ok, what has happened: anything inserted into the DB?
39        $stmt = $db->prepare('SELECT id, label FROM test');
40        $stmt->execute();
41        var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
42
43        // Now the same with emulated PS.
44        $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 1);
45        if (1 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
46            printf("[004] Unable to turn on emulated prepared statements\n");
47
48        // Note that the "named placeholder" is enclosed by double quotes.
49        $stmt = $db->prepare("INSERT INTO test(id, label) VALUES(101, ':placeholder')");
50        // No replacement shall be made
51        $stmt->execute(array(':placeholder' => 'row1'));
52        // Again, I'd like to see an error message
53        if ('00000' !== $stmt->errorCode())
54            printf("[005] Execute has failed, %s %s\n",
55                var_export($stmt->errorCode(), true),
56                var_export($stmt->errorInfo(), true));
57
58        // Now, what do we have in the DB?
59        $stmt = $db->prepare('SELECT id, label FROM test ORDER BY id');
60        $stmt->execute();
61        var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
62
63    } catch (PDOException $e) {
64        printf("[001] %s [%s] %s\n",
65            $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
66    }
67
68    print "done!";
69?>
70--CLEAN--
71<?php
72require __DIR__ . '/mysql_pdo_test.inc';
73$db = MySQLPDOTest::factory();
74$db->exec('DROP TABLE IF EXISTS test');
75?>
76--EXPECTF--
77Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number in %s on line %d
78[003] Execute has failed, 'HY093' array (
79  0 => 'HY093',
80  1 => NULL,
81  2 => NULL,
82)
83array(0) {
84}
85
86Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in %s on line %d
87[005] Execute has failed, 'HY093' array (
88  0 => 'HY093',
89  1 => NULL,
90  2 => NULL,
91)
92array(0) {
93}
94done!
95