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