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