PDA

View Full Version : Cannot Connect to MySQL


kjc
10-03-2002, 04:02 PM
I have created a web site in PHP and MySQL. Things work fine normally, but very occasionally it appears that a connection with the MySQL server cannot be made and as a result of the database driven information on the page does appear by instead I get the following dispalyed whenever information from the database is needed:



quote:
--------------------------------------------------------------------------------
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/u/s/username/public_html/browse_category.php on line 64
--------------------------------------------------------------------------------



Is there any reason why this is happenning. It is very infrequent. The mysql_num_rows checks to find out how many results from the query, so surely the error is because the query could no be run.?.

The code on each page is $conn=db_connect(); which then calls the following connection code from an external file:

quote:
--------------------------------------------------------------------------------

function db_connect()
{
$result= @mysql_pconnect("localhost", "username", "password");
if (!result)
return false;
if (!@mysql_select_db("username"))
return false;
return result;
}
--------------------------------------------------------------------------------



Any ideas?

firepages
10-03-2002, 04:40 PM
"Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource "

means that you have returned an empty result , i.e. there are no rows to show , which will produce the warning you get..
you can use

@mysql_num_rows($result);

to suppress the error, but you still need then to catch the fact that you have an empty result.

if(!@mysql_num_rows($query)){die('result set was empty');}

its vaguely possible that the DB is failing between the query and the num_rows call but highly unlikely that that would produce the error you are getting ,and you would probably in that case get a 'could not connect to database' or 'database has gone away' type error , if the DB is busy you may even get a 'too many conections' type error

chances are you are just running a query that returns no rows

kjc
10-03-2002, 04:54 PM
There are matches to the query though. When I get that error on the page, if I refresh the page then it will work properly and retrueve all the results from the database ok. If there were no matches to the query it would return the number of rows as 0. An error only occurs when there is an error in the query or the database cannot connect. :confused:

firepages
10-03-2002, 07:14 PM
"If there were no matches to the query it would return the number of rows as 0. "

nope, it returns false and then you get the warning message, try this...

if(!@mysql_num_rows($result)){echo mysql_error().$SQL;}

(where $SQL is your SQL query and $result your resourse id)

then you will know for sure :)