Php mysqli_fetch_assoc()不工作

Php mysqli_fetch_assoc()不工作,php,mysqli,Php,Mysqli,有人能调试一下,告诉我为什么不工作吗 <?php include("C:\Wamp\www\system\db\connect.php"); $term = mysqli_real_escape_string($con, $_GET['q']); echo "results for \"".$term."\".<br>"; $sql = "SELECT * FROM `search` WHERE Keywords='%{$term}%' LIMIT 10"; $result =

有人能调试一下,告诉我为什么不工作吗

<?php
include("C:\Wamp\www\system\db\connect.php");
$term = mysqli_real_escape_string($con, $_GET['q']);
echo "results for \"".$term."\".<br>";
$sql = "SELECT * FROM `search` WHERE Keywords='%{$term}%' LIMIT 10";
$result = mysqli_query($con, $sql) or die("<p color=\"#f00\">Could not query database.</p>");
while($row = mysqli_fetch_assoc($result) or die("<p color=\"#f00\">Could not fetch assoc array in database.</p>")) {
    echo $row['Title'];
}
echo json_encode($row['Title']);

mysqli_close($con);
?>

它在mysqli_fetch_assoc函数处停止工作。

如注释中所述,您应该打印实际的mysql错误消息,而不是自定义消息,它将帮助您调试错误

下面是一些修复错误的建议

您的代码应该是:

<?php
include("C:\Wamp\www\system\db\connect.php"); //<-- You should give relative path instead of this one
$term = mysqli_real_escape_string($con, $_GET['q']);
echo "results for \"".$term."\".<br>";
$sql = "SELECT * FROM `search` WHERE Keywords like '%{$term}%' LIMIT 10";
$result = mysqli_query($con, $sql) or die(mysqli_error($con)); //<-- show mysql error instead of custom one
while($row = mysqli_fetch_assoc($result) ) {
    echo $row['Title'];
}
echo json_encode($row['Title']);

mysqli_close($con);
?>
更换这些线路:

$sql = "SELECT * FROM `search` WHERE Keywords='%{$term}%' LIMIT 10";
$result = mysqli_query($con, $sql) or die("<p color=\"#f00\">Could not query database.</p>");
while($row = mysqli_fetch_assoc($result) or die("<p color=\"#f00\">Could not fetch assoc array in database.</p>")) {
   echo $row['Title'];
}
有了这些:

$sql = "SELECT * FROM `search` WHERE Keywords LIKE '%{$term}%' LIMIT 10";
$result = mysqli_query($con, $sql) or die("<p color=\"#f00\">Could not query database.</p>");
while($row = mysqli_fetch_assoc($result) ) {
   echo $row['Title'];
}

我在一个只有一个值的简单表上也遇到了同样的问题,我添加了die错误消息,但它仍然没有为我显示任何内容,尝试回显表中的一个特定行,对我来说,它确实显示了一个结果,所以我认为我的问题在于编码为json。 所以我使用了在 至于你那一行的回音,我试过:

$result = mysqli_query($conn, $query) or die(mysqli_error($conn));

if($result->num_rows > 0){
     while($row= mysqli_fetch_assoc($result)){
        echo($row['name_ofRow']);
   }
}

我希望这能对你有所帮助。

到底是什么不起作用?你连接到数据库了吗?你的结果不是预期的?您有一个php错误?WHERE关键字像,而不是“=”,除非您希望得到两个字符上都有百分比字符的字符串ends@0x1gene我可以很好地连接到数据库。正如标题所说,问题在于mysqli_fetch_assoc函数。该函数返回骰子字符串。@n-dru替换为LIKE没有区别。另外,使用或diemysqli\u错误代替打印自定义错误消息。这将帮助您识别错误。我使用该路径是因为使用相对路径返回无法打开流:没有此类文件或目录,但我已访问浏览器中的目录。返回“null”,如果添加了die,则返回die string。