sporadic "access denied"/"no db selected" errors executing simple query...

From: Date: Thu, 10 Mar 2005 15:37:40 +0000
Subject: sporadic "access denied"/"no db selected" errors executing simple query...
Groups: php.db 
Request: Send a blank email to php-db+get-38853@lists.php.net to get a copy of this message
Greetings, I have a problem which shows up *intermittently* when executing a simple query. Basically, mysql_query() returns no resource *some* of the time. When I trap the error using mysql_error(), I get the following:
        "No Database Selected"
For kicks, I altered the query to use the fully qualified table name (<db>.<table>), and the error changed to:
        "Access denied for user: 'username@%' to database 'db'"
If I continue after the error, this is followed by:
        "PHP Warning: mysql_num_rows(): supplied argument is not a
         valid MySQL result resource in /home/www/..."
I was able to replicate this on a browser as well as using "ab" with no concurrency. The system will sometimes handle 1000 or more queries without any problems, and then a bunch of errors will appear. I cannot roll this out into a production environment until this is fixed. I would really appreciate any advice. Here's the relevant code:
    $conn = mysql_connect(CHAR_DB,CHAR_DB_USER,CHAR_DB_PASS) or die("Cannot connect to db.");
    mysql_select_db(USER_DB, $conn);
    $sql = "SELECT cash FROM cash WHERE id = '$id'";
    $res = mysql_query($sql);
    if (!$res) {
        error_log('Invalid query: ' . mysql_error());
    }
    $numrows = mysql_num_rows($res);
Thanks!

« previous php.db (#38853) next »