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