php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36286 Variable holding query results has to be unset
Submitted: 2006-02-04 14:39 UTC Modified: 2006-02-12 01:00 UTC
Votes:4
Avg. Score:4.0 ± 1.0
Reproduced:4 of 4 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (25.0%)
From: memoimyself at yahoo dot com dot br Assigned:
Status: No Feedback Package: PDO related
PHP Version: 5.1.2 OS: Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2006-02-04 14:39 UTC] memoimyself at yahoo dot com dot br
Description:
------------
When running more than one query in a row with PDO, if the result set is assigned to a variable with the same name (e.g. $result) each time, only the first assignment works properly: from the second time on, the variable will contain an empty result set.

I have tested my code in two different setups: Linux (A2 Web Hosting server running PHP 5.1.2) and Windows XP (my test server, also running PHP 5.1.2). This problem ONLY occurs in the first setup (Linux).

It does not matter which "fetch style" is used.

I have worked around this problem by unsetting the result variable each time as soon as all data has been fetched from it.

Reproduce code:
---------------
$dbh = new PDO(BD_DSN, BD_USERNAME, BD_PWD);

if ($result = $dbh->query('SELECT * FROM table_1'))
{
	$all_rows_1 = $result->fetchAll(PDO::FETCH_OBJ);
}

if ($result = $dbh->query('SELECT * FROM table_2'))
{
	$all_rows_2 = $result->fetchAll(PDO::FETCH_OBJ);
}

Expected result:
----------------
If both tables actually contain data, $all_rows_1 and $all_rows_2 should both contain all the data from each table.

Actual result:
--------------
When code similar to the example is run on my Windows XP test server, everything works as expected.

When, however, the same code is run on the Linux production server, $all_rows_2 will contain an empty array rather than an array with objects representing each row from the table.

If I add unset($result) after each $result->fetchAll(PDO::FETCH_OBJ), then everything works well on the Linux server as well.

Curiously, I have had no problems assigning other types of object the variables with the same name in a sequence or loop (e.g. when creating XML elements in a loop and assigning them to a variable with the same name each time).

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-02-04 18:47 UTC] tony2001@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.1-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5.1-win32-latest.zip


 [2006-02-12 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2006-09-07 14:19 UTC] barry dot verdon at complinet dot com
I have experienced the same thing on 5.1.3 on Debian. Here is the test case I used

$db = new PDO(DSN, USERNAME, PASSWORD);

$stmt = $db->query('SELECT 1+1 AS answer');
$answer = $stmt->fetch(PDO::FETCH_ASSOC); // All fine here

$stmt = $db->query('SELECT 1+2 AS result');
$result = $stmt->fetch(PDO::FETCH_ASSOC); // Result is false


It does the same thing with a foreach loop. But if you named the variables $stmt1 and $stmt2 is would work fine. It even caused a problem when I had a class wrapping, not extending, PDOStatement, slightly different error, on the __call magic method instead, but with different variable names all worked fine.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 01:01:31 2024 UTC