xref: /PHP-8.3/ext/pdo/tests/pdo_012.phpt (revision 902d6439)
1--TEST--
2PDO Common: PDOStatement::setFetchMode
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), grp VARCHAR(10))');
19$db->exec('INSERT INTO test VALUES(1, \'A\', \'Group1\')');
20$db->exec('INSERT INTO test VALUES(2, \'B\', \'Group2\')');
21
22$SELECT = 'SELECT val, grp FROM test';
23
24$stmt = $db->query($SELECT, PDO::FETCH_NUM);
25var_dump($stmt->fetchAll());
26
27class Test
28{
29    public $val;
30    public $grp;
31
32    function __construct($name = 'N/A')
33    {
34        echo __METHOD__ . "($name)\n";
35    }
36}
37
38unset($stmt);
39
40$stmt = $db->query($SELECT, PDO::FETCH_CLASS, 'Test');
41var_dump($stmt->fetchAll());
42
43unset($stmt);
44
45$stmt = $db->query($SELECT, PDO::FETCH_NUM);
46$stmt->setFetchMode(PDO::FETCH_CLASS, 'Test', array('Changed'));
47var_dump($stmt->fetchAll());
48
49?>
50--EXPECTF--
51array(2) {
52  [0]=>
53  array(2) {
54    [0]=>
55    string(1) "A"
56    [1]=>
57    string(6) "Group1"
58  }
59  [1]=>
60  array(2) {
61    [0]=>
62    string(1) "B"
63    [1]=>
64    string(6) "Group2"
65  }
66}
67Test::__construct(N/A)
68Test::__construct(N/A)
69array(2) {
70  [0]=>
71  object(Test)#%d (2) {
72    ["val"]=>
73    string(1) "A"
74    ["grp"]=>
75    string(6) "Group1"
76  }
77  [1]=>
78  object(Test)#%d (2) {
79    ["val"]=>
80    string(1) "B"
81    ["grp"]=>
82    string(6) "Group2"
83  }
84}
85Test::__construct(Changed)
86Test::__construct(Changed)
87array(2) {
88  [0]=>
89  object(Test)#%d (2) {
90    ["val"]=>
91    string(1) "A"
92    ["grp"]=>
93    string(6) "Group1"
94  }
95  [1]=>
96  object(Test)#%d (2) {
97    ["val"]=>
98    string(1) "B"
99    ["grp"]=>
100    string(6) "Group2"
101  }
102}
103