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