php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35993 mysql_query doesnt honor database resource id
Submitted: 2006-01-13 12:08 UTC Modified: 2006-01-14 16:20 UTC
Votes:3
Avg. Score:3.7 ± 1.9
Reproduced:2 of 3 (66.7%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: andreas at fink dot org Assigned:
Status: Not a bug Package: MySQL related
PHP Version: 5.1.2 OS: MacOS X & Linux
Private report: No CVE-ID: None
 [2006-01-13 12:08 UTC] andreas at fink dot org
Description:
------------
When you use multiple connections to mysql databases, you should use a resource id in your query which you got back at the time of connection.

However this doesnt work anymore. The queries always go to one database.

Reproduce code:
---------------
mysqladmin create test1
mysqladmin create test2
mysql  << --EOF--
use test1
CREATE TABLE \`testdata\` (\`id\` int(20) NOT NULL auto_increment,\`data\` varchar(255) NOT NULL default '', PRIMARY KEY  (\`id\`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
grant all on test1.* to 'test'@'localhost' identified by 'test';
insert into test1.testdata(id,data) values (1,'This is DB named test1');
use test2
CREATE TABLE \`testdata\` (\`id\` int(20) NOT NULL auto_increment,\`data\` varchar(255) NOT NULL default '', PRIMARY KEY  (\`id\`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
grant all on test2.* to 'test'@'localhost' identified by 'test';
insert into test2.testdata(id,data) values (1,'This is DB named test2');
--EOF--

now launch php on this:

<?
$db1_handle=mysql_pconnect("127.0.0.1","test","test");
mysql_select_db("test1",$db1_handle);
$db2_handle=mysql_pconnect("127.0.0.1","test","test");
mysql_select_db("test2",$db2_handle);

$query = "select data from testdata where id=1";
$result1 = mysql_query($query,$db1_handle);
$line = mysql_fetch_row ( $result1 );
echo "This query was executed on db1_handle:" .$line[0] ."\n";

$query = "select data from testdata where id=1";
$result2 = mysql_query($query,$db2_handle);
$line = mysql_fetch_row ( $result2 );
echo "This query was executed on db2_handle:" .$line[0] ."\n";
?>


Expected result:
----------------
This query was executed on db1_handle:This is DB named test1
This query was executed on db2_handle:This is DB named test2


Actual result:
--------------
On Linux i386:

This query was executed on db1_handle:This is DB named test2
This query was executed on db2_handle:This is DB named test2

On MacOS X PPC:
This query was executed on db1_handle:This is DB named test1
This query was executed on db2_handle:This is DB named test1

Interesting enough that i386 and ppc are exactly reversed. This might hint to a location which is endian sensitive.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-01-13 12:43 UTC] tony2001@php.net
$db1_handle=mysql_pconnect("127.0.0.1","test","test");
$db2_handle=mysql_pconnect("127.0.0.1","test","test");

These two calls effectively return THE SAME connection identifier (because connect details are the same).
Use mysql_connect() with 4th parameter set to TRUE to force creation of new connection.
No bug here.
 [2006-01-13 15:14 UTC] andreas at fink dot org
Tried the suggestion in two ways: $db1_handle=mysql_connect("127.0.0.1","test","test",1);
mysql_select_db("test1",$db1_handle);
$db2_handle=mysql_connect("127.0.0.1","test","test",1);
mysql_select_db("test2",$db2_handle);

result: 
This query was executed on db1_handle:This is DB named test1
This query was executed on db2_handle:This is DB named test1

so still wrong

if I use mysql_connect instead I get
This query was executed on db1_handle:This is DB named test1

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /Users/afink/test.php on line 14
This query was executed on db2_handle:


So it is still a bug.
 [2006-01-13 17:58 UTC] tony2001@php.net
<?php
$db1_handle=mysql_connect("127.0.0.1","root","", true);
mysql_select_db("test",$db1_handle);
$db2_handle=mysql_connect("127.0.0.1","root","", true);
mysql_select_db("mysql",$db2_handle);

$query = "show tables";
$result1 = mysql_query($query,$db1_handle);

while ($line = mysql_fetch_row ( $result1 ) ) {
    var_dump($line);
}

echo "---------------------------\n";

$query = "show tables";
$result2 = mysql_query($query,$db2_handle);

while ($line = mysql_fetch_row ( $result2 ) ) {
    var_dump($line);
}
?>
This code works perfectly here.
 [2006-01-13 18:30 UTC] tony2001@php.net
>$db1_handle=mysql_pconnect("127.0.0.1","test","test");
>$db2_handle=mysql_pconnect("127.0.0.1","test","test", true);

Do you read what I'm writing or you just ignore it?
Try to open the docs and read whether the 4th parameter of mysql_Pconnect() is the same as the 4th parameter of mysql_Connect().

Again, you're using THE SAME PERSISTENT CONNECTION returned by Pconnect().
Please stop reopening this report, there is no bug.
 [2006-01-13 18:36 UTC] andreas at fink dot org
$db1_handle=mysql_connect("127.0.0.1","test","test");
mysql_select_db("test1",$db1_handle);
$db2_handle=mysql_connect("127.0.0.1","test","test", true);
mysql_select_db("test2",$db2_handle);

gives:

array(1) {
  [0]=>
  string(8) "testdata"
}
---------------------------

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /Users/afink/test2.php on line 19


Now is this a bug or not?
 [2006-01-14 09:34 UTC] georg@php.net
Please stop to reopen this bug, add proper error handling instead to your source and check why you're getting an invalid result.
 [2006-01-14 09:50 UTC] andreas at fink dot org
<?
$db1_handle=mysql_connect("127.0.0.1","test","test");
mysql_select_db("test1",$db1_handle);
$db2_handle=mysql_connect("127.0.0.1","test","test", true);
mysql_select_db("test2",$db2_handle);

$query = "select data from testdata where id=1";
$result1 = mysql_query($query,$db1_handle);
if($result1)
{
	$line = mysql_fetch_row ( $result1 );
	echo "This query was executed on db1_handle:" .$line[0] ."\n";
}
else
	echo mysql_error()."\n";

$query = "select data from testdata2 where id=1";
$result2 = mysql_query($query,$db2_handle);
if($result2)
{
	$line = mysql_fetch_row ( $result2 );
	echo "This query was executed on db2_handle:" .$line[0] ."\n";
}
else
	echo mysql_error()."\n";

?>



Returns:
vpn5:~ afink$ php test.php
This query was executed on db1_handle:This is DB named test1
No database selected
vpn5:~ afink$ 

So what you say now? Is my brain really that damaged that I can not see what's wrong here? I had working code using two concurrent mysql connections not working anymore after upgrading php to a more recent version without doing changes to the code. And I've seen other people with similar problems. I agree that using pconnect in this scenario makes it somehow not obvious as you expect the change of database to be on your resource id only and not on another one (non obvious) but you want to avoid to have to connect/disconnect every time. My thinking was that pconnect returns a connection out of a pool and returns it to the pool at the end and not really using the same one [you can blame me for getting that wrong]. The main reason why you want to have multiple connections is because you read through a list of items on one connection (loop with mysql_fetch_row) while doing other things like queries and inserts on the second conncetion. Doing this on the first connection makes it loose its context of the query. This is obviously a problem for many developers.

However above code shows that there IS DEFINITIVELY a problem.
 [2006-01-14 15:15 UTC] sniper@php.net
What if you actually passed the right connection handle to mysql_error()? And add this as first line in your script: error_reporting(E_ALL); 

btw: It works just fine for me too.
 [2006-01-14 16:14 UTC] andreas at fink dot org
if I pass the mysql handle to mysql_err I still get:

This query was executed on db1_handle:This is DB named test1

Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in /Users/afink/test.php on line 25


I'm using MySQL 5.0.15 btw.
 [2006-01-14 16:20 UTC] sniper@php.net
So you actually don't even get the 2nd handle for some reason.
Please ask further support questions elsewhere. You really need to learn how to debug scripts. This is not a PHP bug.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 21:01:32 2025 UTC