xref: /PHP-8.2/ext/pdo/tests/pdo_016.phpt (revision 74859783)
1--TEST--
2PDO Common: PDO::FETCH_BOUND
3--EXTENSIONS--
4pdo
5--SKIPIF--
6<?php
7$dir = getenv('REDIR_TEST_DIR');
8if (false == $dir) die('skip no driver');
9if (!strncasecmp(getenv('PDOTEST_DSN'), 'oci', strlen('oci'))) die('skip not relevant for oci driver - cannot reexecute after closing cursors without reparse');
10require_once $dir . 'pdo_test.inc';
11PDOTest::skip();
12?>
13--FILE--
14<?php
15if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.__DIR__ . '/../../pdo/tests/');
16require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
17$db = PDOTest::factory();
18
19if ($db->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql') {
20    $db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
21}
22
23$db->exec('CREATE TABLE test(idx int NOT NULL PRIMARY KEY, txt VARCHAR(20))');
24$db->exec('INSERT INTO test VALUES(0, \'String0\')');
25$db->exec('INSERT INTO test VALUES(1, \'String1\')');
26$db->exec('INSERT INTO test VALUES(2, \'String2\')');
27
28$stmt1 = $db->prepare('SELECT COUNT(idx) FROM test');
29$stmt2 = $db->prepare('SELECT idx, txt FROM test ORDER by idx');
30
31$stmt1->execute();
32var_dump($stmt1->fetchColumn());
33$stmt1 = null;
34
35$stmt2->execute();
36$cont = $stmt2->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE);
37var_dump($cont);
38
39echo "===WHILE===\n";
40
41$stmt2->bindColumn('idx', $idx);
42$stmt2->bindColumn('txt', $txt);
43$stmt2->execute();
44
45while($stmt2->fetch(PDO::FETCH_BOUND)) {
46    var_dump(array($idx=>$txt));
47}
48
49echo "===ALONE===\n";
50
51$stmt3 = $db->prepare('SELECT txt FROM test WHERE idx=:inp');
52$stmt3->bindParam(':inp', $idx); /* by foreign name */
53
54$stmt4 = $db->prepare('SELECT idx FROM test WHERE txt=:txt');
55$stmt4->bindParam(':txt', $txt);  /* using same name */
56
57foreach($cont as $idx => $txt)
58{
59    var_dump(array($idx=>$txt));
60    var_dump($stmt3->execute());
61
62    if ($idx == 0) {
63        /* portability-wise, you may only bindColumn()s
64         * after execute() has been called at least once */
65        $stmt3->bindColumn('txt', $col1);
66    }
67    var_dump($stmt3->fetch(PDO::FETCH_BOUND));
68    $stmt3->closeCursor();
69
70    var_dump($stmt4->execute());
71    if ($idx == 0) {
72        /* portability-wise, you may only bindColumn()s
73         * after execute() has been called at least once */
74        $stmt4->bindColumn('idx', $col2);
75    }
76    var_dump($stmt4->fetch(PDO::FETCH_BOUND));
77    $stmt4->closeCursor();
78    var_dump(array($col2=>$col1));
79}
80
81echo "===REBIND/SAME===\n";
82
83$stmt4->bindColumn('idx', $col1);
84
85foreach($cont as $idx => $txt)
86{
87    var_dump(array($idx=>$txt));
88    var_dump($stmt3->execute());
89    var_dump($stmt3->fetch(PDO::FETCH_BOUND));
90    $stmt3->closeCursor();
91    var_dump($col1);
92    var_dump($stmt4->execute());
93    var_dump($stmt4->fetch(PDO::FETCH_BOUND));
94    $stmt4->closeCursor();
95    var_dump($col1);
96}
97
98echo "===REBIND/CONFLICT===\n";
99
100$stmt2->bindColumn('idx', $col1);
101$stmt2->bindColumn('txt', $col1);
102$stmt2->execute();
103
104while($stmt2->fetch(PDO::FETCH_BOUND))
105{
106    var_dump($col1);
107}
108
109
110?>
111--EXPECT--
112string(1) "3"
113array(3) {
114  [0]=>
115  string(7) "String0"
116  [1]=>
117  string(7) "String1"
118  [2]=>
119  string(7) "String2"
120}
121===WHILE===
122array(1) {
123  [0]=>
124  string(7) "String0"
125}
126array(1) {
127  [1]=>
128  string(7) "String1"
129}
130array(1) {
131  [2]=>
132  string(7) "String2"
133}
134===ALONE===
135array(1) {
136  [0]=>
137  string(7) "String0"
138}
139bool(true)
140bool(true)
141bool(true)
142bool(true)
143array(1) {
144  [0]=>
145  string(7) "String0"
146}
147array(1) {
148  [1]=>
149  string(7) "String1"
150}
151bool(true)
152bool(true)
153bool(true)
154bool(true)
155array(1) {
156  [1]=>
157  string(7) "String1"
158}
159array(1) {
160  [2]=>
161  string(7) "String2"
162}
163bool(true)
164bool(true)
165bool(true)
166bool(true)
167array(1) {
168  [2]=>
169  string(7) "String2"
170}
171===REBIND/SAME===
172array(1) {
173  [0]=>
174  string(7) "String0"
175}
176bool(true)
177bool(true)
178string(7) "String0"
179bool(true)
180bool(true)
181string(1) "0"
182array(1) {
183  [1]=>
184  string(7) "String1"
185}
186bool(true)
187bool(true)
188string(7) "String1"
189bool(true)
190bool(true)
191string(1) "1"
192array(1) {
193  [2]=>
194  string(7) "String2"
195}
196bool(true)
197bool(true)
198string(7) "String2"
199bool(true)
200bool(true)
201string(1) "2"
202===REBIND/CONFLICT===
203string(7) "String0"
204string(7) "String1"
205string(7) "String2"
206