mysql查询偶尔不返回任何内容 [英] mysql query occasionally returns nothing

查看:208
本文介绍了mysql查询偶尔不返回任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在PHP/MySQL应用程序中使用了一个函数,该函数返回基本配置信息,它包含一个简单的select查询,如下所示:

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;
}

我们遇到的这个问题是,偶尔,看似随机的,该查询在mysql_result上抛出mysql错误,提示提供的参数不是有效的mysql结果资源".在调试中,我们确定这不是因为未传递$ field.本质上,由于某种原因,我们无法确定完全有效的查询是否失败,并且不返回导致空结果集和后续错误的结果.如果错误是由于mysql连接失败而导致,脚本将在此之前死亡.另外,此功能在某些页面加载时可能会被调用50-100次,但每次加载时往往只会失败一次.

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.

谢谢.

推荐答案

搜索 php提供的参数不是有效的mysql结果资源" 显示,要获取实际的错误,您需要调用mysql_error,您得到的错误是因为查询的结果为FALSE-此值不是有效的mysql结果资源.

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.

鉴于错误是"MySQL服务器已消失",可能有多种原因-搜索还建议一些与php相关的和特定于堆栈的错误,因此您似乎可能需要更加仔细地调试它.

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.

这篇关于mysql查询偶尔不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆