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