views:

298

answers:

1

Hi,

We have a function used within our PHP/MySQL application which returns basic configuration information, it contains a simple select query and looks like this:

public function getConfigurationValue($field)
{
    $res = mysql_query("SELECT `cfg_value` FROM `ls_config` WHERE `cfg_name` = '".mysql_real_escape_string($field)."'");
    $cfg = htmlspecialchars(mysql_result($res,0));
    return $cfg;
}

This problem we are having is that occasionally, seemingly at random, this query throws a mysql error on mysql_result saying that "supplied argument is not a valid mysql result resource". In our debugging we have determined though that this is not because $field is not being passed. Essentially, for a reason we cannot determine a perfectly valid query fails and returns no results causing an empty result set and the subsequent error. If the error was due to the mysql connection failing the script would have died well before this. Also, this function may be called 50-100 times on some page loads but it only tends to fail once on each load.

Please let me know if you need any other information to work this out.

Thanks.

+5  A: 

searching for php "supplied argument is not a valid mysql result resource" reveals that to get the actual error, you'd need to call mysql_error, and the error that you get is because the result of the query is FALSE - this value not being a valid mysql result resource.

i.e. in short you have something like:

$res = FALSE; # should contain the mysql result but does not, due to error.
$cfg = htmlspecialchars(mysql_result($res,0)); # the attempt to call mysql_result on invalid argument errors out.

So you'd want to use something like this:

$query = "SELECT * FROM cats WHERE id=$id";
$qr1 = mysql_query ($query)
      or die ("Query failed: " . mysql_error() . " Actual query: " . $query);

You might want to give this a shot and see what the underlying error message says.


Given that the error is "MySQL server has gone away", There can be multitude of reasons for it - this article would be a good start to investigate. Searching suggests also some php-related and stack-specific bugs, so it looks like you might need to debug it with a closer attention.

Maybe try to duplicate the setup on another box and then start experimenting with the versions/settings, and see if any of the already reported scenarios match your case. Unfortunately, seems there's no single simple answer to this.

Andrew Y
the error is from mysql_result, excerpt from apache error log:mysql_result(): supplied argument is not a valid MySQL result resource ...
seengee
to confirm, in debugging we have emailed ourself the actual SQL query that is being run and it is completely valid
seengee
yes, I've edited my answer to clarify.
Andrew Y
I understand exactly what you mean but I think you are maybe missing the point. I understand that the reason mysql_result is falling over is because the query is returning FALSE but what i'm trying to understand is why the query is returning FALSE when 99.99% of the time it returns a valid result set
seengee
Just add the _or die(...mysql_error()...)_ code and see for yourself.
VolkerK
another developer assured me he had done this and no actual error was being thrown. I have now put this in place and it seems that intermittently we are getting Error 2006: MySQL server has gone away
seengee
@seengee: no worries, hopefully this helps a bit to dig further - looks like it will need a bit more digging. @VolkerK: thx! :-)
Andrew Y