gpt4 book ai didi

php - 如何在 PHP 中显示取决于用户输入的长查询的 MySQL 错误?

转载 作者:行者123 更新时间:2023-11-29 16:12:25 25 4
gpt4 key购买 nike

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

"Query Failed".

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

这是我的代码,

$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));

最佳答案

使用这个:

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

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

echo mysqli_errno($this->db_link);

看看herehere

关于php - 如何在 PHP 中显示取决于用户输入的长查询的 MySQL 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55145695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com