php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #34964 using PDO::query() more than once with MySQL-driver
Submitted: 2005-10-24 03:52 UTC Modified: 2005-10-24 14:10 UTC
From: akorthaus at web dot de Assigned: wez (profile)
Status: Not a bug Package: PDO related
PHP Version: 5.* OS: *
Private report: No CVE-ID: None
 [2005-10-24 03:52 UTC] akorthaus at web dot de
Description:
------------
If I want to use PDO::query() more than one time in a script, I have to set the last PDOStatement to NULL, before I can use the second query. Only If I add:

$stmt1 = NULL;

before

$stmt2 = $db->query ...

in the example below, I can work with the second query. 

But - how can I work with two statments at the same time? I often have to do this! I don't understand it because I use two different variables for the statements here.

I have used the pdo_mysql driver (MySQL 4.1.14), pdo and pdo_mysql were compiled from latest CVS.

Reproduce code:
---------------
<?php
$db = new PDO('...');

$stmt1 = $db->query('SELECT * FROM table1');
var_dump($stmt1);

$stmt2 = $db->query('SELECT * FROM table2');
var_dump($stmt1);
?>

Expected result:
----------------
object(PDOStatement)#2 ...
object(PDOStatement)#2 ...

Actual result:
--------------
object(PDOStatement)#2 ...
bool(false)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-10-24 04:05 UTC] akorthaus at web dot de
that's how this problem could also look like:

<?php
$db = new PDO('...');

$stmt = $db->query('SELECT * FROM table1');
var_dump($stmt);

$stmt = $db->query('SELECT * FROM table2');
var_dump($stmt);

$stmt = $db->query('SELECT * FROM table3');
var_dump($stmt);
?>

output:
-------------------------
object(PDOStatement)#2 ...
bool(false)
object(PDOStatement)#2 ...


Only if I do this:

<?php
$db = new PDO('...');

$stmt = $db->query('SELECT * FROM table1');
var_dump($stmt);
$stmt = NULL;

$stmt = $db->query('SELECT * FROM table2');
var_dump($stmt);
$stmt = NULL;

$stmt = $db->query('SELECT * FROM table3');
var_dump($stmt);
$stmt = NULL;
?>

output:
-------------------------
object(PDOStatement)#2 ...
object(PDOStatement)#2 ...
object(PDOStatement)#2 ...

This seems to work. But should this really be the way to go? I hope it's a but ;-)
 [2005-10-24 10:32 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The MySql driver uses the old mysql protocol which is not capable of this. We need someone to write a mysqlI driver and may want to find a way to issue an error if used like this.
 [2005-10-24 11:23 UTC] akorthaus at web dot de
But why can't the mysql-driver set the last Statement to NULL, perhaps in mysql_handle_doer() - http://cvs.php.net/co.php/php-src/ext/pdo_mysql/mysql_driver.c?r=1.65#236  ?

Shouldn't the API across all drivers be as consistent as possible?

The docs don't mention that you have to set the last statement to NULL ("for one of the drivers"), and don't mention that PDO::query() could return FALSE.

Perhaps at least an exception should be thrown instead of returning FALSE.

And you're right, with another PDO driver like sqlite I get a result like:

output:
---------------------
object(PDOStatement)#2 ...
object(PDOStatement)#3 ...
object(PDOStatement)#4 ...
 [2005-10-24 14:10 UTC] wez@php.net
helly: that's bogus information, this has nothing to do with "mysqli".  We link against the client libraries provided by the user, and those are responsible for talking to the server.

akorthaus:
The reason that you can't run multiple queries without closing the old statement is that mysql doesn't allow this for unbuffered queries.

Portable scripts *must* null out their statement handles between queries, or call PDOStatement::closeCursor(), or just use PDOStatement::fetchAll().

If you want exceptions, turn on exception error mode.

I think you need to read the manual at http://php.net/pdo for more information on this stuff.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Nov 24 06:01:23 2020 UTC