gpt4 book ai didi

PHP 搜索功能返回结果失败

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

我正在重构一个 PHP 脚本(从 mysqli 迁移到 PDO)。它的目的是为网站创建资源列表并允许用户过滤它们。还有一个基本的搜索功能。重构脚本中的所有内容都工作正常,除了搜索功能。尝试搜索不会返回任何结果。信息存在于数据库中,我在 Apache 日志中找不到任何错误。这是代码:

require_once('web_misc_config');

...

$search=(isset($_GET['search']) ? $_GET['search'] : null);
$search= addslashes($search);
$searchletter=(isset($_GET['searchletter']) ? $_GET['searchletter'] : null);

//This while loop creates the searched version of the A to Z list.
if (!empty($search)){
$result = $con->prepare("SELECT title,summary,url,coverage,format FROM

dbs where title like :search or summary like :search");
$result->bindParam(':search', $search, PDO::PARAM_STR);
$result->execute();

while($row = $result->fetch())
{
$url=$row['url'];
$title=$row['title'];
$summary=$row['summary'];
$coverage=$row['coverage'];
$format=$row['format'];

echo ('<p><h6><a href="' . $url . '">' . $title . '</a></h6>
<br />' . $summary . '</p>');
}
}

//This block creates the filtered and searched version of the list.
elseif (!empty($searchletter)) {
$result = $con->prepare("SELECT title,summary,url,coverage,format,fletter FROM dbs where fletter = :searchletter");
$result->bindParam(':searchletter', $searchletter);
$result->execute();

while($row = $result->fetch())
{
$url=$row['url'];
$title=$row['title'];
$summary=$row['summary'];
$coverage=$row['coverage'];
$format=$row['format'];

echo ('<p><h6><a href="' . $url . '">' . $title . '</a></h6>
<br />' . $summary . '</p>');
}
}

//This block loop creates the inital A to Z list.
else {
$result = $con->prepare("SELECT title,summary,url,coverage,format FROM dbs");
$result->execute();

while($row = $result->fetch())
{
$url=$row['url'];
$title=$row['title'];
$summary=$row['summary'];
$coverage=$row['coverage'];
$format=$row['format'];

echo ('<p><h6><a href="' . $url . '">' . $title . '</a></h6>
<br /> ' . $summary . '</p>');
}
}
$result = null;
$con = null;

ELSEIF 和 ELSE block 工作正常。填充了初始的、未过滤的列表,用户可以按字母顺序过滤它。它们包含在这里是为了完整性和比较。问题出在 IF block 中的 while 循环(在第一条评论下)。它评估为 false,导致出现空白屏幕而不是搜索结果。只要从数据库中检索到结果,它就应该评估为真。谁能看到我可能错过的东西?

最佳答案

由于 $search 不包含任何通配符,LIKE 将被视为 = 并查找完全匹配。如果要在列的任意位置搜索it,需要添加通配符。

if (!empty($search)){
$search = "%$search%";
$result = $con->prepare("SELECT title,summary,url,coverage,format
FROM dbs
where title like :search or summary like :search");
$result->bindValue(':search', $search, PDO::PARAM_STR);
$result->execute();

关于PHP 搜索功能返回结果失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53072699/

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