1--TEST--
2MySQL PDO->errorInfo()
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        $info = $obj->errorInfo();
19        $code = $info[0];
20
21        if (($code != $expected) && (($expected != '00000') && ($code != ''))) {
22            printf("[%03d] Expecting error code '%s' got code '%s'\n",
23                $offset, $expected, $code);
24        }
25
26        if ($expected != '00000') {
27            if (!isset($info[1]) || $info[1] == '')
28                printf("[%03d] Driver-specific error code not set\n", $offset);
29            if (!isset($info[2]) || $info[2] == '')
30                printf("[%03d] Driver-specific error message.not set\n", $offset);
31        }
32
33    }
34
35    function pdo_mysql_errorinfo($db, $offset) {
36
37        try {
38
39            /*
40            If you create a PDOStatement object through PDO->prepare()
41            or PDO->query() and invoke an error on the statement handle,
42            PDO->errorCode() will not reflect that error. You must call
43            PDOStatement->errorCode() to return the error code for an
44            operation performed on a particular statement handle.
45            */
46            $code = $db->errorCode();
47            check_error($offset + 2, $db);
48
49            $stmt = $db->query('SELECT id, label FROM test');
50            $stmt2 = &$stmt;
51            check_error($offset + 3, $db);
52            check_error($offset + 4, $stmt);
53
54            $db->exec('DROP TABLE IF EXISTS test');
55            @$stmt->execute();
56            check_error($offset + 5, $db);
57            check_error($offset + 6, $stmt, '42S02');
58            check_error($offset + 7, $stmt2, '42S02');
59
60            @$stmt = $db->query('SELECT id, label FROM unknown');
61            check_error($offset + 8, $db, '42S02');
62
63            MySQLPDOTest::createTestTable($db);
64            $stmt = $db->query('SELECT id, label FROM test');
65            check_error($offset + 9, $db);
66            check_error($offset + 10, $stmt);
67
68            $db2 = &$db;
69            $db->exec('DROP TABLE IF EXISTS unknown');
70            @$db->query('SELECT id, label FROM unknown');
71            check_error($offset + 11, $db, '42S02');
72            check_error($offset + 12, $db2, '42S02');
73            check_error($offset + 13, $stmt);
74            check_error($offset + 14, $stmt2);
75
76            // lets hope this is an invalid attribute code
77            $invalid_attr = -1 * PHP_INT_MAX + 3;
78            $tmp = @$db->getAttribute($invalid_attr);
79            check_error($offset + 15, $db, 'IM001');
80            check_error($offset + 16, $db2, 'IM001');
81            check_error($offset + 17, $stmt);
82            check_error($offset + 18, $stmt2);
83
84        } catch (PDOException $e) {
85            printf("[%03d] %s [%s] %s\n",
86                $offset + 19, $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
87        }
88    }
89
90    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
91    printf("Emulated Prepared Statements...\n");
92    pdo_mysql_errorinfo($db, 0);
93
94    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
95    printf("Native Prepared Statements...\n");
96    pdo_mysql_errorinfo($db, 20);
97
98    print "done!";
99?>
100--CLEAN--
101<?php
102require __DIR__ . '/mysql_pdo_test.inc';
103MySQLPDOTest::dropTestTable();
104?>
105--EXPECT--
106Emulated Prepared Statements...
107[015] Driver-specific error code not set
108[015] Driver-specific error message.not set
109[016] Driver-specific error code not set
110[016] Driver-specific error message.not set
111Native Prepared Statements...
112done!
113