mysqli声明LIKE和BETWEEN

时间:2017-08-30 07:42:40

标签: mysql mysqli

当我想在某个日期之间搜索数据时,亲切的先生是正确的mysqli声明。

实施例。我输入"待定",即reservationstatus然后从日期选择器中选择(2017/8/28),该日期选择器将成为from_date并选择另一个日期(2017/8/30),这将是至今。

结果必须是2017/8/28至2017/8/30之间的待处理预订状态。

先谢谢你,希望你能帮助我。

$query = "
    SELECT * FROM reservations 
    WHERE firstname LIKE '".$search."'
    OR lastname LIKE '".$search."' 
    OR emailaddress LIKE '%".$search."%' 
    OR contactnumber LIKE '%".$search."%' 
    OR speaker LIKE '".$search."'
    OR topic LIKE '%".$search."%' 
    OR location LIKE '%".$search."%' 
    OR audience LIKE '%".$search."%' 
    OR count LIKE '%".$search."%'
    OR time LIKE '%".$search."%' 
    OR date LIKE '%".$search."%' 
    OR reservationdate LIKE '%".$search."%' 
    OR reservationtime LIKE '%".$search."%'
    OR reservationstatus LIKE '%".$search."%'
    AND date BETWEEN '".$_POST["from_date"]."' AND '".$_POST["to_date"]."'";

1 个答案:

答案 0 :(得分:2)

$query = "
    SELECT * FROM reservations 
    WHERE (firstname LIKE '".$search."'
    OR lastname LIKE '".$search."' 
    OR emailaddress LIKE '%".$search."%' 
    OR contactnumber LIKE '%".$search."%' 
    OR speaker LIKE '".$search."'
    OR topic LIKE '%".$search."%' 
    OR location LIKE '%".$search."%' 
    OR audience LIKE '%".$search."%' 
    OR count LIKE '%".$search."%'
    OR time LIKE '%".$search."%' 
    OR date LIKE '%".$search."%' 
    OR reservationdate LIKE '%".$search."%' 
    OR reservationtime LIKE '%".$search."%'
    OR reservationstatus LIKE '%".$search."%')
    AND date BETWEEN '".$_POST["from_date"]."' 
    AND '".$_POST["to_date"]."'";

为所有OR子句添加功能括号将解决该错误。