#43586 [Opn]: Unable to query again after foreach on MySQL

From: Date: Fri, 04 Jan 2008 06:49:43 +0000
Subject: #43586 [Opn]: Unable to query again after foreach on MySQL
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-121022@lists.php.net to get a copy of this message
ID: 43586 User updated by: vicrry at yahoo dot com dot hk Reported By: vicrry at yahoo dot com dot hk Status: Open Bug Type: PDO related Operating System: Linux PHP Version: 5.2.5 New Comment: I found a simple solve for this error. Unset the PDOStatement object each time before your query again with the same variable. Reproduce code: --------------- $Pdo = new PDO('mysql:host=localhost,dbname=test'); $res=$Pdo->query('SELECT * FROM TEST_TABLE'); foreach($res as $row) print_r($row); unset($res); $res=$Pdo->query('SELECT * FROM TEST_TABLE2'); foreach($res as $row) print_r($row); --------------- But this seems to be a bug anyway, it DO causes inconveniences. Previous Comments: ------------------------------------------------------------------------ [2007-12-21 12:52:13] vicrry at yahoo dot com dot hk I believe the following definition will surely cause what I mentioned. -- ------------------------------------ -- TEST_TABLE ( id int(12) PRIMARY KEY ); TEST_TABLE2 ( id int(12) PRIMARY KEY ); -- ------------------------------------ -- In fact it occurs on every foreach I used on any table in my database. ------------------------------------------------------------------------ [2007-12-18 15:17:58] msaraujo@php.net Hello, Can you please send me your SQL definition for TEST_TABLE and TEST_TABLE2? ------------------------------------------------------------------------ [2007-12-13 07:08:36] vicrry at yahoo dot com dot hk Description: ------------ It seems the cursor didn't close after iterated through with foreach statement, but I don't insist any other causes. I don't know if it is expected, but it really cause inconvenience and reduce readability to add a fetchAll() line after every query. Reproduce code: --------------- $Pdo = new PDO('mysql:host=localhost,dbname=test'); $res=$Pdo->query('SELECT * FROM TEST_TABLE'); foreach($res as $row) print_r($row); $res=$Pdo->query('SELECT * FROM TEST_TABLE2'); foreach($res as $row) print_r($row); Expected result: ---------------- Prints out each row in table 'TEST_TABLE' and 'TEST_TABLE2'. Actual result: -------------- The first query seems works fine, though it might have error info left unprinted. The second query doesn't work at all, and the query() statement didn't even report an error. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=43586&edit=1

« previous php.bugs (#121022) next »