在搜索栏上搜索关键字时,显示数据后再次键入另一个关键字未显示显示错误,未找到数据,您可以告诉我[关闭]

when search a keyword on search bar data show after again type another keyword not show show me error no data found can you tell me [closed]

提问人:Nitin Prajapati 提问时间:11/17/2023 更新时间:11/17/2023 访问量:36

问:


想改进这个问题吗?更新问题,使其仅通过编辑这篇文章来关注一个问题。

2天前关闭。

当我使用一个 keyowrd 数据显示时,如何一次搜索多个关键字,然后再次搜索另一个关键字数据未显示

我的代码在下面

public function searchData($searchVal){

        try {

            $dbConnection = $this->dbConnect();
            $stmt = $dbConnection->prepare("SELECT * FROM `tb_report` WHERE `product` like :searchVal OR `maintitle` like :searchVal ORDER BY `id` DESC LIMIT 15");
            $val = "%$searchVal%";  
            $stmt->bindParam(':searchVal', $val, PDO::PARAM_STR);           
            $stmt->execute();
            $Count = $stmt->rowCount(); 
            $result ="" ;
            
            
            
            if ($Count  > 0){
                    $result='<div class="left_cat_name"> Reports</div>';
                while($data=$stmt->fetch(PDO::FETCH_ASSOC)) {                                       
                   $result = $result .'<div class="search-result2"><a class="fineword" href="'.'https://www.website.com/'.$data['product_url'].'/'.'">'.$this->highlightWords($data['product'],$searchVal).'</a></div>';
                   //$result = $result .'<div class="search-result2"><a class="fineword" href="'.'https://www.website.com/'.$data['product_url'].'">'.$data["product"].'</a></div>';
                }
                $result .= '<button class="btn btn-default custom-btn" id="view-result" onclick="myFunction()">View Full Results</button>';
                $result .= '<button class="btn btn-default custom-btn" id="viewresultmob" onclick="mobilekey()">View Full Results</button>';
                return $result;
            }
        }
        catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();
        }
    }   

我需要你的帮助,你能告诉我它是如何工作的吗

php mysql pdo

评论

2赞 CBroe 11/17/2023
如果输入搜索关键字,则只会查找包含该文本序列的记录,这两个单词之间都有一个空格字符。它不会找到任何东西,这些关键字之间有任何其他内容。因此,您需要将搜索词拆分为各个搜索关键字 and ,然后在查询中执行 - 或者使用 AND 而不是 OR,如果您只想查找包含两者的记录。foo barlike :searchValfoobarWHERE field LIKE '%foo%' OR field LIKE '%bar%'
1赞 CBroe 11/17/2023
对于两个以上的关键字,需要相应地附加更多此类条件,因此您需要动态构建查询。
0赞 Nitin Prajapati 11/17/2023
SELECT * FROM WHERE like '%:searchVal%' OR like '%:searchVal%' ORDER BY DESC LIMIT 15 这样可以吗?tb_reportproductmaintitleid
0赞 Nitin Prajapati 11/17/2023
如果可能的话,你能告诉我怎么做吗
1赞 CBroe 11/17/2023
“这样可以吗?”- 不,我只是向你解释了为什么。“如果可能的话,你能告诉我怎么做吗” - 将搜索词拆分为单个单词,然后遍历这些单词,以创建所需的正确查询格式。

答: 暂无答案