mysql query occasionally returns nothing

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 05:47:08

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/1158576/mysql-query-occasionally-returns-nothing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!