xref: /PHP-8.4/ext/pdo/tests/pdo_025.phpt (revision f4a5db3e)
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 test025(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 test025 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 test025');
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--CLEAN--
64<?php
65require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
66$db = PDOTest::factory();
67PDOTest::dropTableIfExists($db, "test025");
68?>
69--EXPECTF--
70object(Test)#%d (3) {
71  ["id"]=>
72  string(2) "10"
73  ["val"]=>
74  string(3) "Abc"
75  ["val2"]=>
76  string(3) "zxy"
77}
78object(Test)#%d (3) {
79  ["id"]=>
80  string(2) "20"
81  ["val"]=>
82  string(3) "Def"
83  ["val2"]=>
84  string(3) "wvu"
85}
86object(Test)#%d (3) {
87  ["id"]=>
88  string(2) "30"
89  ["val"]=>
90  string(3) "Ghi"
91  ["val2"]=>
92  string(3) "tsr"
93}
94object(Test)#%d (3) {
95  ["id"]=>
96  string(2) "40"
97  ["val"]=>
98  string(3) "Jkl"
99  ["val2"]=>
100  string(3) "qpo"
101}
102object(Test)#%d (3) {
103  ["id"]=>
104  string(2) "50"
105  ["val"]=>
106  string(3) "Mno"
107  ["val2"]=>
108  string(3) "nml"
109}
110object(Test)#%d (3) {
111  ["id"]=>
112  string(2) "60"
113  ["val"]=>
114  string(3) "Pqr"
115  ["val2"]=>
116  string(3) "kji"
117}
118===FAIL===
119
120Fatal error: Uncaught Error: Cannot access protected property Fail::$id in %spdo_025.php:%d
121Stack trace:
122#0 {main}
123  thrown in %spdo_025.php on line %d
124