|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24977 mysql_select_db returns 'true' when select failed
Submitted: 2003-08-07 16:29 UTC Modified: 2003-08-08 08:40 UTC
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: jmat at shutdown dot net Assigned: iliaa
Status: Closed Package: MySQL related
PHP Version: 4CVS-2003-08-07 (stable) OS: Linux 2.4.18/Redhat 7.2
Private report: No CVE-ID:
 [2003-08-07 16:29 UTC] jmat at shutdown dot net
When trying to select a database, about every 5 times, the database will not get selected, and future queries will fail.  To make matters worse, mysql_select_db is returning 'true' even when the database isn't selected.  This code has worked since PHP 4.0, and has never changed; this problem just started appearing on the latest CVS (had to upgrade to fix ErrorDocument under Apache/2).

Attached code is pieced together from my database abstraction library; it produces the same problems as my full code does.

I have verified there are no problems with the database server.

When I push this code back on to 4.3.2, I can't make it fail.

PHP Version 4.3.3RC3-dev 
Apache 2.0.47
MySQL Support Client API version 4.0.14 

for full phpinfo:

Reproduce code:
/* DEFINE's for server, server port, username, passwd */

$db_handle = mysql_pconnect(DB_SERVER.":".DB_PORT,DB_USER,DB_PASS))

$RETRY = 0;
while (!mysql_select_db(SITE_DB,$db_handle)) {
    logger("ERROR: Cannot select ".SITE_DB." database! ATTEMPT $RETRY".mysql_error(),0);
    if ($RETRY++ > 5) {
        trigger_error("Couldn't select ".SITE_DB." on ".DB_SERVER,2);
        return false;

$result = mysql_query("select count(*) from user");
if (mysql_error()) die("FAIL: ".mysql_error());
$value = mysql_result($result,0);
echo $value;

Expected result:

Actual result:
about 1/3 the time:
"FAIL:  No Database Selected"

the other 2/3:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2003-08-07 16:39 UTC] jmat at shutdown dot net
It appears that restarting the webserver will 'fix' the problem, for at least a few hours.

While the server in question is working fine now, a second server (semi-production) just started throwing "No Database Selected" about 5 minutes ago -- it's been up for 4 hours.  It's running the same versions (except Redhat 8) of Apache/PHP.

Restarting it fixed the problem on it, as well.

For now, I'll just cron a server restart every 3 hours.  :)
 [2003-08-07 16:48 UTC] jmat at shutdown dot net
Yet some more info:

I just had a 3rd server start throwing "No Database Selected".

Instead of restarting it, I added a small piece of code into my abstract library for running queries.

if (mysql_error() == "No Database Selected") {
    $retry_query = true;

I basically have this enclosed in a while($retry_query == true) loop around my queries...

When I do this, it works fine -- the database gets 'reselected', and it works.

I'm going to leave this in there for now, although it's not very elegant, it will keep me up for now. :)
 [2003-08-07 18:47 UTC]
Try the following patch and see if it fixes the problem (be sure to remove your workaround before you start testing it).
 [2003-08-07 20:53 UTC] jmat at shutdown dot net
That patch appears to have fixed the problem.. 

It's been running for the past two hours -- I used a load tester and pushed 56,000+ pages through it -- before the patch, about 1/3 failed -- after the patch, they all worked:  not a single failure.

I'm going to push this updated version out to my other servers now.
 [2003-08-08 08:40 UTC]
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at
In case this was a documentation problem, the fix will show up soon at

In case this was a website problem, the change will show
up on the site and on the mirror sites in short time.
Thank you for the report, and for helping us make PHP better.

 [2003-11-26 08:16 UTC] gibex at remar dot pascani dot rdsnet dot ro
problem persist in php 4.3.4

$conn = mysql_connect($dbhost, $dbuser, $dbpasswd) or die(mysql_error()); 
mysql_select_db($dbname, $conn) or die(mysql_error());

Can you provide me a solution of this.

PHP Copyright © 2001-2015 The PHP Group
All rights reserved.
Last updated: Mon Nov 30 06:01:32 2015 UTC