PHP搜索功能无法返回结果

时间:2018-10-30 20:49:18

标签: 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块工作正常。最初的未过滤列表已填充,用户可以按字母顺序对其进行过滤。为了完整和比较,将它们包括在此处。问题出在IF块中的while循环中(在第一个注释下)。它的评估结果为false,导致出现空白屏幕而不是搜索结果。只要从数据库中检索结果,它就应该评估为true。谁能看到我可能会错过的东西?

1 个答案:

答案 0 :(得分:3)

由于row.reformat()不包含任何通配符,因此$search将被视为LIKE并寻找完全匹配的字符。如果要在列中的任何位置搜索它,则需要添加通配符。

=