我正在尝试动态创建mysql查询,如下所示,但卡在下面的部分。 不知道这里有什么问题。任何人都可以帮忙
<?php
$rating=$_GET['rating'];
$where = '';
if(isset($_GET['rating'])){
$data = implode(',',$rating);
$where = "AND WHERE rating IN($data)";
}
$query = "select * from shops WHERE city='Mumbai' $where";
答案 0 :(得分:0)
您应该将每个值用单引号括起来,将它们传递到IN
子句中的WHERE
:
$data = is_array($rating) ? "'" . implode("', '",$rating) . "'" : "'$rating'";
同时删除第二个WHERE
:
$where = "AND rating IN($data)";
答案 1 :(得分:0)
您正在使用WHERE两次。从下面拿出来
$where = "AND WHERE rating IN($data)";
应该只是
$where = "AND rating IN($data)";