xref: /php-src/ext/pdo_mysql/tests/bug_44454.phpt (revision 4bb75d56)
1--TEST--
2Bug #44454 (Unexpected exception thrown in foreach() statement)
3--EXTENSIONS--
4pdo_mysql
5--SKIPIF--
6<?php
7require_once __DIR__ . '/inc/mysql_pdo_test.inc';
8MySQLPDOTest::skip();
9?>
10--FILE--
11<?php
12require_once __DIR__ . '/inc/mysql_pdo_test.inc';
13$db = MySQLPDOTest::factory();
14
15function bug_44454($db) {
16    try {
17        $db->exec('DROP TABLE IF EXISTS test_44454');
18        $db->exec('CREATE TABLE test_44454(a INT, b INT, UNIQUE KEY idx_ab (a, b))');
19        $db->exec('INSERT INTO test_44454(a, b) VALUES (1, 1)');
20
21        $stmt = $db->query('SELECT a, b FROM test_44454');
22        printf("... SELECT has returned %d row...\n", $stmt->rowCount());
23        while ($row = $stmt->fetch()) {
24            try {
25                printf("... INSERT should fail...\n");
26                $db->exec('INSERT INTO test_44454(a, b) VALUES (1, 1)');
27            } catch (Exception $e) {
28                printf("... STMT - %s\n", var_export($stmt->errorCode(), true));
29                printf("... PDO  - %s\n", var_export($db->errorInfo(), true));
30            }
31        }
32
33        $db->exec('DROP TABLE IF EXISTS test_44454');
34        $db->exec('CREATE TABLE test_44454(a INT, b INT, UNIQUE KEY idx_ab (a, b))');
35        $db->exec('INSERT INTO test_44454(a, b) VALUES (1, 1)');
36
37    } catch (Exception $e) {
38        printf("... While error %s\n", $e->getMessage()); ;
39    }
40
41    $stmt = $db->query('SELECT a, b FROM test_44454');
42    printf("... SELECT has returned %d row...\n", $stmt->rowCount());
43    foreach ($stmt as $row) {
44        try {
45            printf("... INSERT should fail...\n");
46            $db->exec('INSERT INTO test_44454(a, b) VALUES (1, 1)');
47        } catch (Exception $e) {
48            printf("... STMT - %s\n", var_export($stmt->errorCode(), true));
49            printf("... PDO  - %s\n", var_export($db->errorInfo(), true));
50        }
51    }
52}
53
54$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
55
56print "Native Prepared Statements\n";
57$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
58bug_44454($db);
59
60print "\nEmulated Prepared Statements\n";
61$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
62bug_44454($db);
63
64print "done!";
65?>
66--CLEAN--
67<?php
68require_once __DIR__ . '/inc/mysql_pdo_test.inc';
69$db = MySQLPDOTest::factory();
70$db->exec('DROP TABLE IF EXISTS test_44454');
71?>
72--EXPECTF--
73Native Prepared Statements
74... SELECT has returned 1 row...
75... INSERT should fail...
76... STMT - '00000'
77... PDO  - array (
78  0 => '23000',
79  1 => 1062,
80  2 => 'Duplicate entry \'1-1\' for key %s',
81)
82... SELECT has returned 1 row...
83... INSERT should fail...
84... STMT - '00000'
85... PDO  - array (
86  0 => '23000',
87  1 => 1062,
88  2 => 'Duplicate entry \'1-1\' for key %s',
89)
90
91Emulated Prepared Statements
92... SELECT has returned 1 row...
93... INSERT should fail...
94... STMT - '00000'
95... PDO  - array (
96  0 => '23000',
97  1 => 1062,
98  2 => 'Duplicate entry \'1-1\' for key %s',
99)
100... SELECT has returned 1 row...
101... INSERT should fail...
102... STMT - '00000'
103... PDO  - array (
104  0 => '23000',
105  1 => 1062,
106  2 => 'Duplicate entry \'1-1\' for key %s',
107)
108done!
109