douping4436 2018-10-30 20:49
浏览 97
已采纳

PHP搜索功能无法返回结果

I am in the process of refactoring a PHP script (moving from mysqli to PDO). It's purpose is to create a list of resources for a website and allow users to filter them. There is also a basic search function. Everything in the refactored script is working fine, except the search function. Attempting to search returns no results. The information is present in the database and I can't find any errors in the Apache logs. Here is the code:

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;

The ELSEIF and ELSE blocks work fine. The initial, unfiltered list is populated and users can filter it alphabetically. They are included here for completeness and comparison. The problem is with the while loop in the IF block (under the first comment). It is evaluating to false causing a blank screen to appear instead of search results. It should evaluate to true as long as results are retrieved from the database. Can anyone see something I might have missed?

  • 写回答

1条回答 默认 最新

  • dppx9253 2018-10-30 21:17
    关注

    Since $search doesn't contain any wildcard characters, LIKE will be treated like = and look for an exact match. If you want to search for the it anywhere in the column, you need to add wildcards.

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R