如何在 PHP 中为依赖于用户输入的长查询显示 MySQL 错误?

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

How do I display a MySQL error in PHP for a long query that depends on the user input?

phpmysql

提问by Traveling Salesman

In PHP, I am trying to execute a long MySQL query that depends on the user input. However, my query fails with the following message,

在 PHP 中,我试图执行一个依赖于用户输入的长 MySQL 查询。但是,我的查询失败并显示以下消息,

"Query Failed".

Actually I have printed this message whenever the query fails, but I am having hard time looking for the reason behind this failure. Unfortunately, I couldn't find it because the error is not specified on the web page. Is there a way to display the error message that caused the failure on the web page?

实际上,每当查询失败时,我都会打印此消息,但是我很难找到此失败背后的原因。不幸的是,我找不到它,因为网页上没有指定错误。有没有办法在网页上显示导致失败的错误信息?

Here's my code,

这是我的代码,

$from = "Findings";
$where = "";

if ($service != null)
{
    $from = $from . ", ServiceType_Lookup";
    $where= "Findings.ServiceType_ID= ServiceType_Lookup.ServiceType_ID AND ServiceType_Name= ". $service;

    if ($keyword != null)
        $where= $where . " AND ";
}

if ($keyword != null)
{
    $where= $where . "Finding_ID LIKE '%$keyword%' OR
                     ServiceType_ID LIKE '%$keyword%' OR
                     Title LIKE '%$keyword%' OR
                     RootCause_ID LIKE '%$keyword%' OR
                     RiskRating_ID LIKE '%$keyword%' OR
                     Impact_ID LIKE '%$keyword%' OR
                     Efforts_ID LIKE '%$keyword%' OR
                     Likelihood_ID LIKE '%$keyword%' OR
                     Finding LIKE '%$keyword%' OR
                     Implication LIKE '%$keyword%' OR
                     Recommendation LIKE '%$keyword%' OR
                     Report_ID LIKE '%$keyword%'";
}

$query = "SELECT Finding_ID,
                 ServiceType_ID,
                 Title,
                 RootCause_ID,
                 RiskRating_ID,
                 Impact_ID,
                 Efforts_ID,
                 Likelihood_ID,
                 Finding,
                 Implication,
                 Recommendation,
                 Report_ID  FROM ".$from . " WHERE " . $where;

echo "wala 2eshiq";

$this->result = $this->db_link->query($query);
if (!$this->result) {
    printf("Query failed: %s\n", mysqli_connect_error());
    exit;
}

$r = mysqli_query($this->db_link, $query);
if ($r == false)
    printf("error: %s\n", mysqli_errno($this->db_link));

回答by Christian

Use this:

用这个:

mysqli_query($this->db_link, $query) or die(mysqli_error($this->db_link)); 
# mysqli_query($link,$query) returns 0 if there's an error.
# mysqli_error($link) returns a string with the last error message

You can also use this to print the error code.

您也可以使用它来打印错误代码。

echo mysqli_errno($this->db_link);

Take a look hereand here

看看这里这里

回答by Julien

Use function die():

使用函数die()

or die(mysql_error());

回答by Arian Faurtosh

I use the following to turn all error reporting on for MySQLi

我使用以下命令为 MySQLi 打开所有错误报告

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

*NOTE:don't use this in a production environment.

*注意:不要在生产环境中使用它。

回答by mc_fish

The suggestions don't work because they are for the standard MySQL driver, not for mysqli:

这些建议不起作用,因为它们适用于标准 MySQL 驱动程序,而不适用于 mysqli:

$this->db_link->errorcontains the error if one did occur

$this->db_link->error如果确实发生了错误,则包含错误

Or

或者

mysqli_error($this->db_link)

will work.

将工作。

回答by Abdel Rahman Karim

Try something like this:

尝试这样的事情:

$link = @new mysqli($this->host, $this->user, $this->pass)
$statement = $link->prepare($sqlStatement);
                if(!$statement)
                {
                    $this->debug_mode('query', 'error', '#Query Failed<br/>' . $link->error);
                    return false;
                }

回答by Hrvoje Antunovi?

One useful line of code for you would be:

对您有用的一行代码是:

$sql = "Your SQL statement here";
$result = mysqli_query($this->db_link, $sql) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error($this->db_link), E_USER_ERROR);

This method is better than die, because you can use it for development AND production. It's the permanent solution.

此方法优于die,因为您可以将其用于开发和生产。这是永久解决方案。