Php Sql查询许多类似WHERE的字段
我正在编写一个搜索程序,该搜索程序使用城市代码(或id_city)的条件,这意味着结果必须仅来自所选的一个或多个城市代码。因此,我为城市id_city=1和id_city=2传递值1,为城市id_city=3和id_city=4传递值2Php Sql查询许多类似WHERE的字段,php,mysqli,where,sql-like,Php,Mysqli,Where,Sql Like,我正在编写一个搜索程序,该搜索程序使用城市代码(或id_city)的条件,这意味着结果必须仅来自所选的一个或多个城市代码。因此,我为城市id_city=1和id_city=2传递值1,为城市id_city=3和id_city=4传递值2 if ($city==1){ $setzone = "citycode in ('1','2')"; } elseif($city==2){ $setzone = "citycode in ('3','4')";} 变量$setzone包含我在sql中使用的数
if ($city==1){
$setzone = "citycode in ('1','2')"; }
elseif($city==2){
$setzone = "citycode in ('3','4')";}
变量$setzone包含我在sql中使用的数字,其中“citycode”是我表中的一个字段
因此,在前端,用户可以在不同的“字段”之间搜索,以获得诸如名称、位置、区域、食物之类的结果
在我的咨询中,我有这样的想法:
$restaurants=$mysqli->query("select id, name,zone, ocassion, citycode,
kf.kindfood as food from restaurants as r
inner join kind_food as kf on r.foodcode=kf.id_food
where ".$setzone."
and name like '%".$mysqli->real_escape_string($search)."%'
or zone like '%".$mysqli->real_escape_string($search)."%'
or ocassion like '%".$mysqli->real_escape_string($seach)."%'
or kindfood like '%".$mysqli->real_escape_string($search)."%'
order by id desc");
问题是,此咨询只能搜索$setzone中的名称,但如果我搜索其他内容,如kindfood、ocassion、zone,则会得到所有结果并忽略$setzone。括号是您的朋友:
$restaurants=$mysqli->query("select id, name,zone, ocassion, citycode,
kf.kindfood as food from restaurants as r
inner join kind_food as kf on r.foodcode=kf.id_food
where ".$setzone."
here --> and (name like '%".$mysqli->real_escape_string($search)."%'
or zone like '%".$mysqli->real_escape_string($search)."%'
or ocassion like '%".$mysqli->real_escape_string($seach)."%'
or kindfood like '%".$mysqli->real_escape_string($search)."%'
and here --> ) order by id desc");
如果没有这两个括号,或
过滤器总是获胜。使用,将它们聚集在一起,仍然需要$setzone
.括号,括号,括号谢谢。现在我可以睡觉了。你好,我觉得这很简单。我真的很感激你的帮助。现在我可以睡觉了。问候。@Christian,你可以接受答案并将其标记为已解决,以下是解决方法