1--TEST--
2MySQL PDO->errorInfo()
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_errorinfo';
16    MySQLPDOTest::createTestTable($table, $db);
17
18    function check_error($offset, &$obj, $expected = '00000') {
19        $info = $obj->errorInfo();
20        $code = $info[0];
21
22        if (($code != $expected) && (($expected != '00000') && ($code != ''))) {
23            printf("[%03d] Expecting error code '%s' got code '%s'\n",
24                $offset, $expected, $code);
25        }
26
27        if ($expected != '00000') {
28            if (!isset($info[1]) || $info[1] == '')
29                printf("[%03d] Driver-specific error code not set\n", $offset);
30            if (!isset($info[2]) || $info[2] == '')
31                printf("[%03d] Driver-specific error message.not set\n", $offset);
32        }
33    }
34
35    function pdo_mysql_errorinfo($db, $offset) {
36        global $table;
37
38        try {
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 {$table}");
50            $stmt2 = &$stmt;
51            check_error($offset + 3, $db);
52            check_error($offset + 4, $stmt);
53
54            $db->exec("DROP TABLE IF EXISTS {$table}");
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($table, $db);
64            $stmt = $db->query("SELECT id, label FROM {$table}");
65            check_error($offset + 9, $db);
66            check_error($offset + 10, $stmt);
67
68            $db2 = &$db;
69            $db->exec('DROP TABLE IF EXISTS pdo_mysql_errorinfo_unknown');
70            @$db->query('SELECT id, label FROM pdo_mysql_errorinfo_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_once __DIR__ . '/inc/mysql_pdo_test.inc';
103$db = MySQLPDOTest::factory();
104$db->query('DROP TABLE IF EXISTS pdo_mysql_errorinfo');
105?>
106--EXPECT--
107Emulated Prepared Statements...
108[015] Driver-specific error code not set
109[015] Driver-specific error message.not set
110[016] Driver-specific error code not set
111[016] Driver-specific error message.not set
112Native Prepared Statements...
113done!
114