SQLで複数のキーワード検索

サンプルコード:

if(count($newstr)==1){
$sql = “select * from tb_info where title like '%".$newstr[0]."%’ or content like '%".$newstr[0]."%’order by id desc “;
}else{

//クエリの結果集合をマージ
for($i=0;$i<count($newstr);$i++){
$sql0.=" title like '%".trim($newstr[$i])."%'"." or";
}
for($j=0;$j<count($newstr);$j++){
$sql1.=" content like '%".trim($newstr[$j])."%'"." or";
}
$sql1=substr($sql1,0,-3); //最後"or"を削除
$sql="select * from tb_info where".$sql0.$sql1." order by id desc";

}

Source

Posted by arkgame