xref: /PHP-8.0/ext/pdo/tests/pdo_038.phpt (revision a5cf8280)
1--TEST--
2PDOStatement::fetchColumn() invalid column index
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
16function fetchColumn($stmt, $index) {
17    $stmt->execute();
18    return $stmt->fetchColumn($index);
19}
20
21$conn  = PDOTest::factory();
22$query = 'SELECT 1';
23
24switch ($conn->getAttribute(PDO::ATTR_DRIVER_NAME)) {
25    case 'oci':
26        $query .= ' FROM DUAL';
27        break;
28    case 'firebird':
29        $query .= ' FROM RDB$DATABASE';
30        break;
31}
32
33$stmt = $conn->prepare($query);
34
35try {
36    var_dump(fetchColumn($stmt, -1));
37} catch (\ValueError $e) {
38    echo $e->getMessage(), \PHP_EOL;
39}
40var_dump(fetchColumn($stmt, 0));
41try {
42    var_dump(fetchColumn($stmt, 1));
43} catch (\ValueError $e) {
44    echo $e->getMessage(), \PHP_EOL;
45}
46?>
47--EXPECT--
48Column index must be greater than or equal to 0
49string(1) "1"
50Invalid column index
51