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