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