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