xref: /PHP-5.5/ext/pdo/tests/pdo_025.phpt (revision 30d290a3)
1--TEST--
2PDO Common: PDO::FETCH_INTO
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');
8require_once $dir . 'pdo_test.inc';
9PDOTest::skip();
10?>
11--FILE--
12<?php
13if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.dirname(__FILE__) . '/../../pdo/tests/');
14require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
15$db = PDOTest::factory();
16
17$db->exec('CREATE TABLE test(id INT NOT NULL PRIMARY KEY, val VARCHAR(10), val2 VARCHAR(16))');
18
19$data = array(
20    array('10', 'Abc', 'zxy'),
21    array('20', 'Def', 'wvu'),
22    array('30', 'Ghi', 'tsr'),
23    array('40', 'Jkl', 'qpo'),
24    array('50', 'Mno', 'nml'),
25    array('60', 'Pqr', 'kji'),
26);
27
28
29// Insert using question mark placeholders
30$stmt = $db->prepare("INSERT INTO test VALUES(?, ?, ?)");
31foreach ($data as $row) {
32    $stmt->execute($row);
33}
34
35class Test {
36	public $id, $val, $val2;
37}
38
39$stmt = $db->prepare('SELECT * FROM test');
40$stmt->setFetchMode(PDO::FETCH_INTO, new Test);
41$stmt->execute();
42
43foreach($stmt as $obj) {
44	var_dump($obj);
45}
46
47echo "===FAIL===\n";
48
49class Fail {
50	protected $id;
51	public $val, $val2;
52}
53
54$stmt->setFetchMode(PDO::FETCH_INTO, new Fail);
55$stmt->execute();
56
57foreach($stmt as $obj) {
58	var_dump($obj);
59}
60
61?>
62--EXPECTF--
63object(Test)#%d (3) {
64  ["id"]=>
65  string(2) "10"
66  ["val"]=>
67  string(3) "Abc"
68  ["val2"]=>
69  string(3) "zxy"
70}
71object(Test)#%d (3) {
72  ["id"]=>
73  string(2) "20"
74  ["val"]=>
75  string(3) "Def"
76  ["val2"]=>
77  string(3) "wvu"
78}
79object(Test)#%d (3) {
80  ["id"]=>
81  string(2) "30"
82  ["val"]=>
83  string(3) "Ghi"
84  ["val2"]=>
85  string(3) "tsr"
86}
87object(Test)#%d (3) {
88  ["id"]=>
89  string(2) "40"
90  ["val"]=>
91  string(3) "Jkl"
92  ["val2"]=>
93  string(3) "qpo"
94}
95object(Test)#%d (3) {
96  ["id"]=>
97  string(2) "50"
98  ["val"]=>
99  string(3) "Mno"
100  ["val2"]=>
101  string(3) "nml"
102}
103object(Test)#%d (3) {
104  ["id"]=>
105  string(2) "60"
106  ["val"]=>
107  string(3) "Pqr"
108  ["val2"]=>
109  string(3) "kji"
110}
111===FAIL===
112
113Fatal error: Cannot access protected property Fail::$id in %spdo_025.php on line %d%A
114