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