php 如何检查使用旧 API 的 MySQL 查询是否成功?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11918797/
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 02:26:56  来源:igfitidea点击:

How to check if a MySQL query using the legacy API was successful?

phpmysql

提问by JohnSmith

How do I check if a MySQL query is successful other than using die()

除了使用之外,如何检查 MySQL 查询是否成功 die()

I'm trying to achieve...

我正在努力实现...

mysql_query($query);

if(success){
//move file
}
else if(fail){
//display error
}

回答by Mark Byers

This is the first example in the manual page for mysql_query:

这是手册页中的第一个示例mysql_query

$result = mysql_query('SELECT * WHERE 1=1');
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

If you wish to use something other than die, then I'd suggest trigger_error.

如果你想使用除 之外的其他东西die,那么我建议trigger_error.

回答by Taha Paksu

You can use mysql_errno()for this too.

你也可以用mysql_errno()这个。

$result = mysql_query($query);

if(mysql_errno()){
    echo "MySQL error ".mysql_errno().": "
         .mysql_error()."\n<br>When executing <br>\n$query\n<br>";
}

回答by ?_?

If your query failed, you'll receive a FALSE return value. Otherwise you'll receive a resource/TRUE.

如果您的查询失败,您将收到 FALSE 返回值。否则,您将收到资源/TRUE。

$result = mysql_query($query);

if(!$result){
    /* check for error, die, etc */
}

Basically as long as it's not false, you're fine. Afterwards, you can continue your code.

基本上只要不是假的就没事。之后,您可以继续您的代码。

if(!$result)

This part of the code actually runs your query.

这部分代码实际上运行您的查询。

回答by Rubin Porwal

mysql_queryfunction is used for executing mysql query in php. mysql_queryreturns false if query execution fails.Alternatively you can try using mysql_error()function For e.g

mysql_query函数用于在php中执行mysql查询。mysql_query如果查询执行失败,则返回 false。或者,您可以尝试使用mysql_error()函数例如

$result=mysql_query($sql)

or

或者

die(mysql_error());

In above code snippet if query execution fails then it will terminate the execution and display mysql error while execution of sql query.

在上面的代码片段中,如果查询执行失败,那么它将终止执行并在执行 sql 查询时显示 mysql 错误。

回答by layouteam

put only :

只放:

or die(mysqli_error());

or die(mysqli_error());

after your query

在您查询之后

and it will retern the error as echo

它会将错误重新显示为 echo

example

例子

// "Your Query" means you can put "Select/Update/Delete/Set" queries here
$qfetch = mysqli_fetch_assoc(mysqli_query("your query")) or die(mysqli_error()); 



    if (mysqli_errno()) {
        echo 'error' . mysqli_error();
        die();
    }

回答by Yousef Altaf

if using MySQLibind_param try to put this line above the query

如果使用MySQLibind_param 尝试将此行放在查询上方

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);