php 捕获 mysql_query 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5019160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:33:09  来源:igfitidea点击:

Catching mysql_query error

phpmysql

提问by cds5059

I am trying to stop the mysql_query error from being output onto my form. Currently if there is no location found, I receive the error

我试图阻止 mysql_query 错误输出到我的表单上。目前如果没有找到位置,我会收到错误

"Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 11"

“警告:mysql_result() [function.mysql-result]:无法跳转到 MySQL 结果索引 11 上的第 0 行”

I am trying to catch this error, and instead assign the $location variable to not found. My code for attempting this is below, what am I doing wrong?

我试图捕捉这个错误,而是将 $location 变量分配给 not found。我的尝试代码如下,我做错了什么?

Thanks!

谢谢!

$query3 = "SELECT `location` FROM location WHERE vin = '$vin' LIMIT 1";
$result3 = mysql_query($query3);

if (!mysql_result($result3,0)) {
    $location = "Not found";
} else $location = mysql_result($result3,0,0);

回答by Marc B

mysql_result()generally shouldn't be used. You'd be better off with something like:

mysql_result()一般不应使用。您最好使用以下内容:

$result3 = mysql_query($query3) or die(mysql_error());

if (mysql_numrows($result3) == 0) then
   $location = "not found";
} else {
   $row = mysql_fetch_array($result3);
   $location = $row[0];
}

Your error is caused by the fact that the query returned no rows - e.g. nothing matched. You then tried to retrieve the first field in the first row of that result set, a row which doesn't exist. Checking the number of returned rows with mysql_numrows()is safer, as that works whether the query found nothing or a bajillion rows.

您的错误是由于查询没有返回任何行 - 例如没有匹配的事实引起的。然后,您尝试检索该结果集第一行中的第一个字段,该行不存在。检查返回的行数mysql_numrows()更安全,因为无论查询未发现任何内容还是大量行,这都有效。

回答by artyom.stv

First, you can add @:

首先,您可以添加@:

if (!@mysql_result($result3,0)) {
    $location = "Not found";
} else $location = mysql_result($result3,0,0);

Second, you can check mysql_num_rows(result3)before mysql_resultcall.

其次,您可以mysql_num_rows(result3)mysql_result致电前检查。

回答by picus

You should look into how to set your error and warning levels in php ini - usually you want a s little output on prod as possible.

您应该研究如何在 php ini 中设置错误和警告级别 - 通常您希望在 prod 上输出尽可能少。

http://www.php.net/manual/en/errorfunc.configuration.php#ini.error-reporting

http://www.php.net/manual/en/errorfunc.configuration.php#ini.error-reporting

However, here, the code that would generate that error is:

但是,在这里,会产生该错误的代码是:

$result3 = mysql_query($query3);

That is the line you should be writing your if or "or die" statements around:

这就是你应该写下 if 或“or die”语句的那一行:

$result3 = mysql_query($query3)or die($location = "not found");

回答by Yahel

Mysql_query returns false if nothing is found so a simple :

如果没有找到任何东西,Mysql_query 返回 false 如此简单:

$result3 = mysql_query($query3);

if (mysql_affected_rows($result3) == 0) {
    $location = "Not found";
} else  $location = mysql_result($result3,0,0);

Should do it.

应该做。

回答by tcarter2005

You should look into using OOP; using a database class to handle interaction with your DB.

您应该考虑使用 OOP;使用数据库类来处理与数据库的交互。

But, basically you want to check if there are any rows, before trying to bring back the results.

但是,基本上您想在尝试返回结果之前检查是否有任何行。

Try checking with "mysql_num_rows" in your "if" statement:

尝试在“if”语句中检查“mysql_num_rows”:

if (!mysql_num_rows($result3)) {