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