mysql_select_db returns 'true' when select failed
| Bug #24977 | mysql_select_db returns 'true' when select failed | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Submitted: | 2003-08-07 16:29 UTC | Modified: | 2003-08-08 08:40 UTC |
|
||||||||||
| From: | jmat at shutdown dot net | Assigned: | iliaa (profile) | |||||||||||
| 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: | None | |||||||||||
[2003-08-07 16:29 UTC] jmat at shutdown dot net
Description: ------------ 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 MYSQL_MODULE_TYPE external for full phpinfo: http://shutdown.net/~web/index.php Reproduce code: --------------- /* DEFINE's for server, server port, username, passwd */ define("SITE_DB","test"); $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); sleep(1); 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: ---------------- 5 Actual result: -------------- about 1/3 the time: "FAIL: No Database Selected" the other 2/3: "5"
Patches
Pull Requests
History
AllCommentsChangesGit/SVN commits
[2003-08-07 16:39 UTC] jmat at shutdown dot net
[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") { mysql_select_db(SITE_DB,$db_handle); $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 20:53 UTC] jmat at shutdown dot net
[2003-08-08 08:40 UTC] iliaa@php.net
[2003-11-26 08:16 UTC] gibex at remar dot pascani dot rdsnet dot ro