gpt4 book ai didi

php - 我的网站搜索没有回显任何结果

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

我进行了网站搜索,它没有回显任何结果,但显示了有多少结果

这是代码:

<?php
$db = new mysqli('localhost','root','root','search');
if (isset($_GET['input'])) {
$keywords = $db->escape_string($_GET['input']);

$query = $db->query("
SELECT Name, ProjectNumber, Hazard, Datedone
FROM hira
WHERE Name LIKE '%{$keywords}%'
OR ProjectNumber LIKE '%{$keywords}%'
");
?>
<div class="result-count">
Found <?php echo $query->num_rows; ?> results
</div>
<?php
if ($query->num_rows) {
while ($r = $query->fetch_object()) {

}
?>
<div class="results">
<h3><?php echo $r->Hazard; ?></h3>
</div>
<?php
}
}
?>
<div class="results">
<h3><?php echo $r->Hazard; ?></h3>
</div>

当我搜索“Andreas”时我得到的结果是“找到 2 个结果”但实际结果并没有显示

我做错了什么提前致谢

最佳答案

我不太确定这是否是你的结果应该是什么样子,因为你的代码中有一些错误。但这将为您提供表格中的条目。

您的错误是直接关闭 while 循环,因此您在实际使用之前释放了 $r 上的结果。

$db = new mysqli('localhost', 'root', 'root', 'search');

if (isset($_GET['input'])) {
$keywords = $db->escape_string($_GET['input']);
$query = $db->query("
SELECT Name, ProjectNumber, Hazard, Datedone
FROM hira
WHERE Name LIKE '%{$keywords}%'
OR ProjectNumber LIKE '%{$keywords}%'");
?>
<div class="result-count">
Found <?php echo $query->num_rows; ?> results
</div>
<?php
if ($query->num_rows) {
while ($r = $query->fetch_object()) {
?>
<div class="results">
<h3><?php echo $r->Hazard; ?></h3>
</div>
<?php
}
}
}

更好一点就是这个解决方案。在您的代码中,您已经转义输入,使用准备好的语句仍然更安全:

$db = new mysqli('localhost', 'root', 'root', 'search');

if (isset($_GET['input'])) {
$keywords = "%" . $_GET['input'] . "%";
$query = $db->prepare("
SELECT Name, ProjectNumber, Hazard, Datedone
FROM hira
WHERE Name LIKE ?
OR ProjectNumber LIKE ?");
$query->bind_param("ss", $keywords, $keywords);
$query->execute();
$query->store_result();
?>
<div class="result-count">
Found <?php echo $query->num_rows; ?> results
</div>
<?php
$query->bind_result($name, $projectNumber, $hazard, $dateDone);
while ($query->fetch()) {
?>
<div class="results">
<h3><?php echo $hazard; ?></h3>
</div>
<?php
}
}

关于php - 我的网站搜索没有回显任何结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41519827/

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