1--TEST--
2MySQL PDO->prepare(), native PS, clear line after error
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    $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
15
16    try {
17        $db->exec(sprintf('CREATE TABLE test_prepare_native_clear_error(id INT, label CHAR(255)) ENGINE=%s', PDO_MYSQL_TEST_ENGINE));
18
19        // We need to run the emulated version first. Native version will cause a fatal error
20        $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 1);
21        if (1 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
22            printf("[002] Unable to turn on emulated prepared statements\n");
23
24        // INSERT a single row
25        $db->exec("INSERT INTO test_prepare_native_clear_error(id, label) VALUES (1, 'row1')");
26
27        $stmt = $db->prepare('SELECT unknown_column FROM test_prepare_native_clear_error WHERE id > :placeholder ORDER BY id ASC');
28        $stmt->execute(array(':placeholder' => 0));
29        if ('00000' !== $stmt->errorCode())
30            printf("[003] Execute has failed, %s %s\n",
31                var_export($stmt->errorCode(), true),
32                var_export($stmt->errorInfo(), true));
33
34        $stmt = $db->prepare('SELECT id, label FROM test_prepare_native_clear_error WHERE id > :placeholder ORDER BY id ASC');
35        $stmt->execute(array(':placeholder' => 0));
36        if ('00000' !== $stmt->errorCode())
37            printf("[004] Execute has failed, %s %s\n",
38                var_export($stmt->errorCode(), true),
39                var_export($stmt->errorInfo(), true));
40        var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
41
42        // Native PS
43        $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0);
44        if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
45            printf("[005] Unable to turn off emulated prepared statements\n");
46
47        $stmt = $db->prepare('SELECT unknown_column FROM test_prepare_native_clear_error WHERE id > :placeholder ORDER BY id ASC');
48        $stmt->execute(array(':placeholder' => 0));
49        if ('00000' !== $stmt->errorCode())
50            printf("[006] Execute has failed, %s %s\n",
51                var_export($stmt->errorCode(), true),
52                var_export($stmt->errorInfo(), true));
53
54        $stmt = $db->prepare('SELECT id, label FROM test_prepare_native_clear_error WHERE id > :placeholder ORDER BY id ASC');
55        $stmt->execute(array(':placeholder' => 0));
56        if ('00000' !== $stmt->errorCode())
57            printf("[007] Execute has failed, %s %s\n",
58                var_export($stmt->errorCode(), true),
59                var_export($stmt->errorInfo(), true));
60        var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
61    } catch (PDOException $e) {
62        printf("[001] %s [%s] %s\n",
63            $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
64    }
65
66    print "done!";
67?>
68--CLEAN--
69<?php
70require_once __DIR__ . '/inc/mysql_pdo_test.inc';
71$db = MySQLPDOTest::factory();
72$db->exec('DROP TABLE IF EXISTS test_prepare_native_clear_error');
73?>
74--EXPECTF--
75Warning: PDOStatement::execute(): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'unknown_column' in 'field list' in %s on line %d
76[003] Execute has failed, '42S22' array (
77  0 => '42S22',
78  1 => 1054,
79  2 => 'Unknown column \'unknown_column\' in \'field list\'',
80)
81array(1) {
82  [0]=>
83  array(2) {
84    ["id"]=>
85    string(1) "1"
86    ["label"]=>
87    string(4) "row1"
88  }
89}
90
91Warning: PDO::prepare(): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'unknown_column' in 'field list' in %s on line %d
92
93Fatal error: Uncaught Error: Call to a member function execute() on false in %s:%d
94Stack trace:
95#0 {main}
96  thrown in %s on line %d
97