使用PDO prepare语句创建搜索结果会出错

时间:2018-05-15 09:07:52

标签: php search

当我使用时

if(!in_array($search_each, $commonwords))

此代码在没有它的情况下给出错误它工作正常 我想创建搜索结果,但不包括一些常用词,如果不使用常用词,此代码可以正常使用...

以下是从数据库中获取的搜索结果

$t=strtolower($_POST['e']);
$commonwords = 'a,an,and,I,it,is,do,does,for,from,go,how,the,flat,offers,offer,etc';

$commonwords = explode(",", $commonwords);

if( strlen( $t ) <= 10 )
    echo "";
else {

    $search_exploded = explode ( " ", $t );
    $construct = '';
    foreach( $search_exploded as $search_each ) {
        if(!in_array($search_each, $commonwords)){
            $construct .="AND d_title LIKE ? ";
         }
    }

    include $_SERVER['DOCUMENT_ROOT'] . "/deal/database/db.php";
    $query =$con1->prepare("SELECT * 
                            FROM deals 
                            WHERE 1 $construct 
                            ORDER BY `d_id` DESC ");

    $params = array_map(function($term) { return "%$term%"; }, 
    $search_exploded);
    $query->execute($params);
    $found=$query->rowCount();
    if($found == 0){
        echo "no Result Found"; 
    }else{ 
        while($row_de=$query->fetch(PDO::FETCH_ASSOC)){
            $de_id=$row_de['d_id'];
            $de_title=$row_de['d_title'];
            $d_logo=$row_de['d_logo']   ;       
            $d_type=$row_de['d_type'];  
            echo  $de_title;
        }

1 个答案:

答案 0 :(得分:0)

if(!in_array($ search_each,$ commonwords)) 以上这行很好,

认为在$ search_exploded中会出现问题,请检查此变量是否有值 $ search_exploded = explode(&#34;&#34;,$ t);