1--TEST--
2MySQL PDO->prepare(), native PS, named placeholder II
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    $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
16
17    try {
18
19        $db->exec('DROP TABLE IF EXISTS test');
20        $db->exec(sprintf('CREATE TABLE test(id INT, label1 CHAR(255), label2 CHAR(255)) ENGINE=%s', PDO_MYSQL_TEST_ENGINE));
21
22        $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0);
23        if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
24            printf("[002] Unable to turn off emulated prepared statements\n");
25        printf("Native...\n");
26
27        // INSERT a single row
28        $stmt = $db->prepare('INSERT INTO test(id, label1, label2) VALUES (1, :placeholder, :placeholder)');
29
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, label1, label2 FROM test WHERE id = 1');
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        printf("Emulated...\n");
46
47        $stmt = $db->prepare('INSERT INTO test(id, label1, label2) VALUES(2, :placeholder, :placeholder)');
48        // No replacement shall be made
49        $stmt->execute(array(':placeholder' => 'row2'));
50        if ('00000' !== $stmt->errorCode())
51            printf("[005] Execute has failed, %s %s\n",
52                var_export($stmt->errorCode(), true),
53                var_export($stmt->errorInfo(), true));
54
55        // Now, what do we have in the DB?
56        $stmt = $db->prepare('SELECT id, label1, label2 FROM test WHERE id = 2');
57        $stmt->execute();
58        var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
59
60        //
61        // Another variation of the theme
62        //
63
64        $db->exec('DELETE FROM test');
65        $db->exec("INSERT INTO test (id, label1, label2) VALUES (1, 'row1', 'row2')");
66        $sql = "SELECT id, label1 FROM test WHERE id = :placeholder AND label1 = (SELECT label1 AS 'SELECT' FROM test WHERE id = :placeholder)";
67
68        // emulated...
69        $stmt = $db->prepare($sql);
70        $stmt->execute(array(':placeholder' => 1));
71        if ('00000' !== $stmt->errorCode())
72            printf("[006] Execute has failed, %s %s\n",
73                var_export($stmt->errorCode(), true),
74                var_export($stmt->errorInfo(), true));
75        var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
76
77        // native...
78        $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0);
79        if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
80            printf("[007] Unable to turn off emulated prepared statements\n");
81        printf("Native...\n");
82
83        $stmt = $db->prepare($sql);
84        $stmt->execute(array(':placeholder' => 1));
85        if ('00000' !== $stmt->errorCode())
86            printf("[008] Execute has failed, %s %s\n",
87                var_export($stmt->errorCode(), true),
88                var_export($stmt->errorInfo(), true));
89        var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
90
91
92    } catch (PDOException $e) {
93        printf("[001] %s [%s] %s\n",
94            $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
95    }
96
97    print "done!";
98?>
99--CLEAN--
100<?php
101require __DIR__ . '/mysql_pdo_test.inc';
102$db = MySQLPDOTest::factory();
103$db->exec('DROP TABLE IF EXISTS test');
104?>
105--EXPECTF--
106Native...
107
108Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number in %s on line %d
109[003] Execute has failed, 'HY093' array (
110  0 => 'HY093',
111  1 => NULL,
112  2 => NULL,
113)
114array(0) {
115}
116Emulated...
117array(1) {
118  [0]=>
119  array(3) {
120    ["id"]=>
121    string(1) "2"
122    ["label1"]=>
123    string(4) "row2"
124    ["label2"]=>
125    string(4) "row2"
126  }
127}
128array(1) {
129  [0]=>
130  array(2) {
131    ["id"]=>
132    string(1) "1"
133    ["label1"]=>
134    string(4) "row1"
135  }
136}
137Native...
138
139Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number in %s on line %d
140[008] Execute has failed, 'HY093' array (
141  0 => 'HY093',
142  1 => NULL,
143  2 => NULL,
144)
145array(0) {
146}
147done!
148