xref: /php-src/ext/pdo/tests/pdo_013.phpt (revision c1fec9bb)
1--TEST--
2PDO Common: PDOStatement iterator
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 test013(id int NOT NULL PRIMARY KEY, val VARCHAR(10), grp VARCHAR(10))');
19$db->exec("INSERT INTO test013 VALUES(1, 'A', 'Group1')");
20$db->exec("INSERT INTO test013 VALUES(2, 'B', 'Group2')");
21
22$SELECT = 'SELECT val, grp FROM test013';
23
24$stmt = $db->prepare($SELECT);
25
26$stmt->execute();
27$stmt->setFetchMode(PDO::FETCH_NUM);
28foreach ($stmt as $data)
29{
30    var_dump($data);
31}
32
33class TestClass
34{
35    public $val;
36    public $grp;
37
38    function __construct($name = 'N/A')
39    {
40        echo __METHOD__ . "($name)\n";
41    }
42}
43
44unset($stmt);
45
46foreach ($db->query($SELECT, PDO::FETCH_CLASS, TestClass::class) as $data)
47{
48    var_dump($data);
49}
50
51unset($stmt);
52
53$stmt = $db->query($SELECT, PDO::FETCH_CLASS, TestClass::class, array('WOW'));
54
55foreach($stmt as $data)
56{
57    var_dump($data);
58}
59?>
60--CLEAN--
61<?php
62require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
63$db = PDOTest::factory();
64PDOTest::dropTableIfExists($db, "test013");
65?>
66--EXPECTF--
67array(2) {
68  [0]=>
69  string(1) "A"
70  [1]=>
71  string(6) "Group1"
72}
73array(2) {
74  [0]=>
75  string(1) "B"
76  [1]=>
77  string(6) "Group2"
78}
79TestClass::__construct(N/A)
80object(TestClass)#%d (2) {
81  ["val"]=>
82  string(1) "A"
83  ["grp"]=>
84  string(6) "Group1"
85}
86TestClass::__construct(N/A)
87object(TestClass)#%d (2) {
88  ["val"]=>
89  string(1) "B"
90  ["grp"]=>
91  string(6) "Group2"
92}
93TestClass::__construct(WOW)
94object(TestClass)#%d (2) {
95  ["val"]=>
96  string(1) "A"
97  ["grp"]=>
98  string(6) "Group1"
99}
100TestClass::__construct(WOW)
101object(TestClass)#%d (2) {
102  ["val"]=>
103  string(1) "B"
104  ["grp"]=>
105  string(6) "Group2"
106}
107