我是否可以知道在变量为空时是否返回数据库中的所有值并在变量具有值时返回特定值?

当变量为空时,我尝试返回所有清真餐厅和非清真餐厅,并在该变量具有值时返回特定值。我的代码看起来像

$halal="Halal";
$category=" ";

    $sql = "SELECT Restaurant.*, Images.thumbnail_url
     from Restaurant
     LEFT JOIN Images ON Images.id =
     (select id from Images
     where Images.image_id = Restaurant.id
     limit 1)
     where MATCH (RestaurantName,RestaurantLocation)      AGAINST('$search_query') and  ( RestaurantHalal = CASE WHEN '$halal' IS NULL   THEN RestaurantHalal  ELSE '$halal' END)
     and Restaurant.is_active = 1";

最佳答案

对不起,我很粗心,只需添加另一个或将其设置为空即可解决整个问题。

 $halal="";
 $category=" ";

$sql = "SELECT Restaurant.*, Images.thumbnail_url
 from Restaurant
 LEFT JOIN Images ON Images.id =
 (select id from Images
 where Images.image_id = Restaurant.id
 limit 1)
 where MATCH (RestaurantName,RestaurantLocation) AGAINST('$search_query') and  ( RestaurantHalal = CASE WHEN '$halal' IS NULL OR '$halal' = ' ' THEN RestaurantHalal  ELSE '$halal' END)
 and Restaurant.is_active = 1";

08-07 05:33