|
| 1 | +--TEST-- |
| 2 | +GH-20214 (PDO::FETCH_DEFAULT unexpected behavior with PDOStatement::setFetchMode) |
| 3 | +--EXTENSIONS-- |
| 4 | +pdo |
| 5 | +--SKIPIF-- |
| 6 | +<?php |
| 7 | +$dir = getenv('REDIR_TEST_DIR'); |
| 8 | +if (false == $dir) die('skip no driver'); |
| 9 | +require_once $dir . 'pdo_test.inc'; |
| 10 | +PDOTest::skip(); |
| 11 | +?> |
| 12 | +--FILE-- |
| 13 | +<?php |
| 14 | +if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.__DIR__ . '/../../pdo/tests/'); |
| 15 | +require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc'; |
| 16 | +$db = PDOTest::factory(); |
| 17 | + |
| 18 | +$db->exec('CREATE TABLE gh20214 (c1 VARCHAR(10), c2 VARCHAR(10))'); |
| 19 | +$db->exec("INSERT INTO gh20214 VALUES ('v1', 'v2')"); |
| 20 | + |
| 21 | +$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); |
| 22 | + |
| 23 | +// setFetchMode with FETCH_DEFAULT should use connection default (FETCH_OBJ) |
| 24 | +$stmt = $db->query('SELECT c1, c2 FROM gh20214'); |
| 25 | +$stmt->setFetchMode(PDO::FETCH_DEFAULT); |
| 26 | +$row = $stmt->fetch(); |
| 27 | +var_dump($row instanceof stdClass); |
| 28 | +var_dump($row->c1); |
| 29 | + |
| 30 | +// fetch with FETCH_DEFAULT should also use connection default |
| 31 | +$stmt = $db->query('SELECT c1, c2 FROM gh20214'); |
| 32 | +$row = $stmt->fetch(PDO::FETCH_DEFAULT); |
| 33 | +var_dump($row instanceof stdClass); |
| 34 | + |
| 35 | +// fetchAll with FETCH_DEFAULT should also use connection default |
| 36 | +$stmt = $db->query('SELECT c1, c2 FROM gh20214'); |
| 37 | +$rows = $stmt->fetchAll(PDO::FETCH_DEFAULT); |
| 38 | +var_dump($rows[0] instanceof stdClass); |
| 39 | + |
| 40 | +// setFetchMode then fetch without argument |
| 41 | +$stmt = $db->query('SELECT c1, c2 FROM gh20214'); |
| 42 | +$stmt->setFetchMode(PDO::FETCH_DEFAULT); |
| 43 | +$row = $stmt->fetch(); |
| 44 | +var_dump($row instanceof stdClass); |
| 45 | + |
| 46 | +// query() with FETCH_DEFAULT as second argument |
| 47 | +$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_NUM); |
| 48 | +$stmt = $db->query('SELECT c1, c2 FROM gh20214', PDO::FETCH_DEFAULT); |
| 49 | +$row = $stmt->fetch(); |
| 50 | +var_dump(is_array($row) && isset($row[0])); |
| 51 | +?> |
| 52 | +--CLEAN-- |
| 53 | +<?php |
| 54 | +require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc'; |
| 55 | +$db = PDOTest::factory(); |
| 56 | +PDOTest::dropTableIfExists($db, "gh20214"); |
| 57 | +?> |
| 58 | +--EXPECT-- |
| 59 | +bool(true) |
| 60 | +string(2) "v1" |
| 61 | +bool(true) |
| 62 | +bool(true) |
| 63 | +bool(true) |
| 64 | +bool(true) |
0 commit comments