1--TEST--
2MySQL PDOStatement->bindParam()
3--EXTENSIONS--
4pdo_mysql
5--SKIPIF--
6<?php
7require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
8MySQLPDOTest::skip();
9?>
10--FILE--
11<?php
12    require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
13    $db = MySQLPDOTest::factory();
14    $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
15
16    MySQLPDOTest::createTestTable($db);
17
18    function pdo_mysql_stmt_bindparam($db, $offset) {
19
20        $stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? ORDER BY id ASC LIMIT 2');
21        $in = 0;
22        if (!$stmt->bindParam(1, $in))
23            printf("[%03d + 1] Cannot bind parameter, %s %s\n", $offset,
24                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
25
26        $stmt->execute();
27        $id = $label = null;
28
29        if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
30            printf("[%03d + 2] Cannot bind integer column, %s %s\n", $offset,
31                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
32
33        if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
34            printf("[%03d + 3] Cannot bind string column, %s %s\n", $offset,
35                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
36
37        while ($stmt->fetch(PDO::FETCH_BOUND))
38            printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
39                $in,
40                var_export($id, true), gettype($id),
41                var_export($label, true), gettype($label));
42
43        printf("Same again...\n");
44        $stmt->execute();
45        while ($stmt->fetch(PDO::FETCH_BOUND))
46            printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
47                $in,
48                var_export($id, true), gettype($id),
49                var_export($label, true), gettype($label));
50
51        // NULL values
52        printf("NULL...\n");
53        $stmt = $db->prepare('INSERT INTO test(id, label) VALUES (100, ?)');
54        $label = null;
55        if (!$stmt->bindParam(1, $label))
56            printf("[%03d + 4] Cannot bind parameter, %s %s\n", $offset,
57                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
58
59        if (!$stmt->execute())
60            printf("[%03d + 5] Cannot execute statement, %s %s\n", $offset,
61                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
62
63        /* NOTE: you cannot use PDO::query() with unbuffered, native PS - see extra test */
64        $stmt = $db->prepare('SELECT id, NULL AS _label FROM test WHERE label IS NULL');
65        $stmt->execute();
66
67        $id = $label = 'bogus';
68        if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
69            printf("[%03d + 6] Cannot bind NULL column, %s %s\n", $offset,
70                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
71
72        if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
73            printf("[%03d + 3] Cannot bind string column, %s %s\n", $offset,
74                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
75
76        while ($stmt->fetch(PDO::FETCH_BOUND))
77            printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
78                $in,
79                var_export($id, true), gettype($id),
80                var_export($label, true), gettype($label));
81    }
82
83    try {
84        printf("Emulated PS...\n");
85        $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 1);
86        if (1 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
87            printf("[002] Unable to turn on emulated prepared statements\n");
88
89        printf("Buffered...\n");
90        $db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
91        pdo_mysql_stmt_bindparam($db, 3);
92
93        printf("Unbuffered...\n");
94        MySQLPDOTest::createTestTable($db);
95        $db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
96        pdo_mysql_stmt_bindparam($db, 4);
97
98        printf("Native PS...\n");
99        $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0);
100        if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
101            printf("[004] Unable to turn off emulated prepared statements\n");
102
103        printf("Buffered...\n");
104        MySQLPDOTest::createTestTable($db);
105        $db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
106        pdo_mysql_stmt_bindparam($db, 5);
107
108        printf("Unbuffered...\n");
109        MySQLPDOTest::createTestTable($db);
110        $db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
111        pdo_mysql_stmt_bindparam($db, 6);
112
113    } catch (PDOException $e) {
114        printf("[001] %s [%s] %s\n",
115            $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
116    }
117
118    print "done!";
119?>
120--CLEAN--
121<?php
122require __DIR__ . '/mysql_pdo_test.inc';
123MySQLPDOTest::dropTestTable();
124?>
125--EXPECT--
126Emulated PS...
127Buffered...
128in = 0 -> id = 1 (integer) / label = 'a' (string)
129in = 0 -> id = 2 (integer) / label = 'b' (string)
130Same again...
131in = 0 -> id = 1 (integer) / label = 'a' (string)
132in = 0 -> id = 2 (integer) / label = 'b' (string)
133NULL...
134in = 0 -> id = 100 (integer) / label = NULL (NULL)
135Unbuffered...
136in = 0 -> id = 1 (integer) / label = 'a' (string)
137in = 0 -> id = 2 (integer) / label = 'b' (string)
138Same again...
139in = 0 -> id = 1 (integer) / label = 'a' (string)
140in = 0 -> id = 2 (integer) / label = 'b' (string)
141NULL...
142in = 0 -> id = 100 (integer) / label = NULL (NULL)
143Native PS...
144Buffered...
145in = 0 -> id = 1 (integer) / label = 'a' (string)
146in = 0 -> id = 2 (integer) / label = 'b' (string)
147Same again...
148in = 0 -> id = 1 (integer) / label = 'a' (string)
149in = 0 -> id = 2 (integer) / label = 'b' (string)
150NULL...
151in = 0 -> id = 100 (integer) / label = NULL (NULL)
152Unbuffered...
153in = 0 -> id = 1 (integer) / label = 'a' (string)
154in = 0 -> id = 2 (integer) / label = 'b' (string)
155Same again...
156in = 0 -> id = 1 (integer) / label = 'a' (string)
157in = 0 -> id = 2 (integer) / label = 'b' (string)
158NULL...
159in = 0 -> id = 100 (integer) / label = NULL (NULL)
160done!
161