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