Php 使用PDO prepare语句创建搜索结果时出错
当我使用Php 使用PDO prepare语句创建搜索结果时出错,php,search,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 = e
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;
}
$t=strtolower($_POST['e']);
$commonwords='a,an,and,I,it,is,do,do,for,from,go,how,the,the,flat,offers,offer,etc';
$commonwords=爆炸(“,”,$commonwords);
如果(strlen($t)准备(“选择”)
来自交易
其中1$construct
由"d_id"DESC)订购;;
$params=array_map(函数($term){返回“%$term%”;},
$search_);
$query->execute($params);
$found=$query->rowCount();
如果($found==0){
回显“未找到结果”;
}否则{
while($row_de=$query->fetch(PDO::fetch_ASSOC)){
$d_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_标题;
}
如果(!在数组中($search\u each,$commonwords))
这条线很好用
所以认为$search_中会出现问题,检查这个变量是否有值
$search\u exploded=exploded(“,$t);一些合理的代码缩进将是一个好主意。它有助于我们阅读代码,更重要的是,它将帮助您调试代码,为您自己的利益着想。您可能会被要求在几周/几个月内修改此代码,最后您将感谢我。在使用前在准备好的语句中打印查询,并在问题中显示。