ID: 35793
Comment by: qlogix at gmail dot com
Reported By: deadman_great at mail dot ru
Status: Assigned
Bug Type: PDO related
Operating System: RH Fedora Core 2
PHP Version: 5CVS-2005-12-25 (snap)
Assigned To: Wez
New Comment:
I can confirm the statement on Centos 4.1, PHP 5.1.2, Mysql 4.1.16
"You cannot use the same variable for a PDOStatement object twice. As
others have pointed out it works when you set this variable to null in
between."
<?php
$db = new PDO(SQL_DSN,SQL_USER,SQL_PASSWORD);
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$result = $db->query('SELECT COUNT(*) FROM Locations');
echo $result."<br>";
$row = $result->fetchAll(PDO::FETCH_ASSOC);
/* Comment the next line out and script returns an error */
//$result = null;
$result = $db->query('SELECT COUNT(*) FROM Accounts');
echo $result."<br>";
$row = $result->fetch(PDO::FETCH_ASSOC); /* This line causes the error
*/
?>
With line "$result = null;" commented out:
Object id #2
Object id #3
PDOException Object
(
[message:protected] => SQLSTATE[HY000]: General error: 2050
With line "$result = null;" not commented out:
Object id #2
Object id #2
No error message (script works)
Previous Comments:
------------------------------------------------------------------------
[2006-03-21 18:37:15] email at steffenweber dot net
I can confirm that this error does not occur on Windows XP + PHP 5.1.2
+ MySQL 5.0.18. It does happen for me on Gentoo Linux + PHP 5.1.2 +
MySQL 4.1.16.
You cannot use the same variable for a PDOStatement object twice. As
others have pointed out it works when you set this variable to null in
between.
------------------------------------------------------------------------
[2006-03-18 22:09:27] gg15 at gmx dot net
$result = $db->query('SELECT COUNT(*) FROM XYZ');
$row = $result->fetch(PDO::FETCH_ASSOC);
$result->closeCursor();
$result = $db->query('SELECT * FROM XYZ');
$row = $result->fetch(PDO::FETCH_ASSOC); // this one throws the error
an $result = null; between the statements fixes the issue, so I think
this is a problem of php...
------------------------------------------------------------------------
[2006-03-11 10:42:51] peres dot yzqk8 at mailnull dot com
I'd got same problem with this piece of code:
$s = $db->query("SELECT * FROM test ORDER BY poledrugie;");
var_dump($s->fetchAll());
$s = $db->query("SELECT * FROM test ORDER BY poletrzecie;");
var_dump($s->fetchAll());
Changed it to:
$s = $db->query("SELECT * FROM test ORDER BY poledrugie;");
var_dump($s->fetchAll());
$st = $db->query("SELECT * FROM test ORDER BY poletrzecie;");
var_dump($st->fetchAll());
So I think it's wrong use of PHP objects...
------------------------------------------------------------------------
[2006-02-14 16:06:00] m at tacker dot org
I can reproduce this bug on two machines (athlon-xp and pentium3) with
PHP 5.1.1-gentoo (cli) (built: Feb 10 2006 18:06:50)
Zend Engine v2.1.0
This is the test case:
<?php
/**
* Test case for bug #35793
* @see http://bugs.php.net/bug.php?id=35793
*
* @author Markus Tacker <[EMAIL PROTECTED]>
* @version $Id: pdo-proxy-bug.php 760 2006-02-14 14:59:19Z mtacker
$
*/
// If you set this to true I will reconnect before each select
// at line 56
// => no crash
$reconnect_before_select = false;
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1);
$dsn = array(
'dbtype' => 'mysql',
'host' => 'localhost',
'database' => 'test',
'username' => 'test',
'password' => '',
);
// $DB = new Database;
$DB = new PDO($dsn['dbtype'] . ':host=' . $dsn['host'] . ';dbname='
. $dsn['database'], $dsn['username'], $dsn['password']);
$table = uniqid();
// Create a test table
$sql = 'CREATE TABLE `' . $table . '` ('
. "varname varchar(64) NOT NULL default '',"
. "varvalue tinytext NOT NULL default '',"
. 'PRIMARY KEY (varname)'
. ') ENGINE=InnoDB DEFAULT CHARSET=utf8';
$result = $DB->exec($sql);
if ($result === false and $result->errorCode() != PDO::ERR_NONE) {
exit('Query failed: ' . $sql . "\n");
}
echo 'OK: ' . $sql . "\n";
// Insert into test table
$sql = 'INSERT INTO `' . $table . '`'
. ' (varname, varvalue)'
. ' VALUES (' . $DB->quote('uniqid') . ', ' . $DB->quote($table) .
')';
$result = $DB->exec($sql);
if ($result === false and $result->errorCode() != PDO::ERR_NONE) {
exit('Query failed: ' . $sql . "\n");
}
echo 'OK: ' . $sql . "\n";
// Select from table
for ($i = 0; $i < 10; $i++) {
if ($reconnect_before_select) {
unset($DB);
$DB = new PDO($dsn['dbtype'] . ':host=' . $dsn['host'] .
';dbname=' . $dsn['database'], $dsn['username'], $dsn['password']);
}
$sql = 'SELECT * FROM `' . $table . '` LIMIT 1';
$result = $DB->query($sql);
if ($result === false or $result->errorCode() != PDO::ERR_NONE)
{
exit('Query failed: ' . $sql . "\n");
}
echo 'OK: ' . $sql . "\n";
$row = $result->fetchObject();
// $row = $result->fetch();
// $row = $result->fetchAll();
if ($row === false or $result->errorCode() != PDO::ERR_NONE) {
$info = $result->errorInfo();
exit('Fetch failed: ' . $info[2] . ' (' . $info[0] . '/' .
$info[1] . ')' . "\n");
}
echo 'OK: ' . $table . ' == ' . $row->varvalue . "\n";
$result->closeCursor();
}
// Delete temp table
$DB->exec('DROP TABLE `' . $table . '`');
?>
------------------------------------------------------------------------
[2006-01-24 11:04:37] [EMAIL PROTECTED]
This is not a mysql bug:
mysql_stmt_fetch returns 1 (error). (Error message "Row retrieval was
canceled by mysql_stmt_close() call")
So pdo called mysql_stmt_close before (I assume prepare/bind/execute
failed before)
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/35793
--
Edit this bug report at http://bugs.php.net/?id=35793&edit=1