1--TEST--
2MySQL PDO->errorCode()
3--SKIPIF--
4<?php
5require_once(__DIR__ . DIRECTORY_SEPARATOR . 'skipif.inc');
6require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
7MySQLPDOTest::skip();
8$db = MySQLPDOTest::factory();
9?>
10--FILE--
11<?php
12    require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
13    $db = MySQLPDOTest::factory();
14    MySQLPDOTest::createTestTable($db);
15
16    function check_error($offset, &$obj, $expected = '00000') {
17
18        $code = $obj->errorCode();
19        if (($code != $expected) && (($expected != '00000') && ($code != ''))) {
20            printf("[%03d] Expecting error code '%s' got code '%s'\n",
21                $offset, $expected, $code);
22        }
23
24    }
25
26    try {
27
28        /*
29        If you create a PDOStatement object through PDO->prepare()
30        or PDO->query() and invoke an error on the statement handle,
31        PDO->errorCode() will not reflect that error. You must call
32        PDOStatement->errorCode() to return the error code for an
33        operation performed on a particular statement handle.
34        */
35        $code = $db->errorCode();
36        check_error(2, $db);
37
38        $stmt = $db->query('SELECT id, label FROM test');
39        $stmt2 = &$stmt;
40        check_error(3, $db);
41        check_error(4, $stmt);
42
43        $db->exec('DROP TABLE IF EXISTS test');
44        @$stmt->execute();
45        check_error(4, $db);
46        check_error(5, $stmt, '42S02');
47        check_error(6, $stmt2, '42S02');
48
49        $db->exec('DROP TABLE IF EXISTS unknown');
50        @$stmt = $db->query('SELECT id, label FROM unknown');
51        check_error(7, $db, '42S02');
52
53        MySQLPDOTest::createTestTable($db);
54        $stmt = $db->query('SELECT id, label FROM test');
55        check_error(8, $db);
56        check_error(9, $stmt);
57
58        $db2 = &$db;
59        @$db->query('SELECT id, label FROM unknown');
60        check_error(10, $db, '42S02');
61        check_error(11, $db2, '42S02');
62        check_error(12, $stmt);
63        check_error(13, $stmt2);
64
65        // lets hope this is an invalid attribute code
66        $invalid_attr = -1 * PHP_INT_MAX + 3;
67        $tmp = @$db->getAttribute($invalid_attr);
68        check_error(14, $db, 'IM001');
69        check_error(15, $db2, 'IM001');
70        check_error(16, $stmt);
71        check_error(17, $stmt2);
72
73    } catch (PDOException $e) {
74        printf("[001] %s [%s] %s\n",
75            $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
76    }
77
78    print "done!";
79?>
80--CLEAN--
81<?php
82require __DIR__ . '/mysql_pdo_test.inc';
83MySQLPDOTest::dropTestTable();
84?>
85--EXPECT--
86done!
87