PHP 搜索功能返回结果失败

标签 php mysql pdo

我正在重构一个 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/

相关文章:

java - SQL/JAVA JDBC通讯链接偶尔出现错误

javascript - 连续滚动。 AJAX、PHP、JAVASCRIPT、MYSQL

php - 无法捕获 MongoConnectionException

php - 如何在数组中查找找到的 id 的索引

php - 需要更快的 PHP/MySQL 搜索算法来进行高度复杂的计算

php - 扩展 PDO 语句类

php - MySQL/PHP根据时间戳返回特定时间(NOW())参数内的帖子

javascript - 将所有 Ajax 代码放在一个 PHP 文件中是不是很糟糕?

php - 如何在 Laravel 中使用 mysql concat 查询?

php - 如何使用 PDO 从 MySQL 获取正确的数据类型?