2012-05-25 94 views
1

以下是我的sql查詢中facility_id我wat分配值來自$ fid。請讓我知道什麼是錯用下面的代碼,所以我可以刪除它們相應sql查詢中的小修改需求

$fid = 101; 
$q = 'select r_present,r_reminder 
    from z_events 
    where resident_id = ? 
     and r_added > date_sub(curdate(), interval 37 day) 
     and fascility_id='."$fid". ' 
    group by r_added 
    order by r_added 
    desc limit 4'; 

回答

1

請試試這個:

$fid = 101; 
    $q = "select r_present,r_reminder from z_events where resident_id = ? and r_added > date_sub(curdate(), interval 37 day) and fascility_id=$fid group by r_added order by r_added desc limit 4"; 
+0

這是很好的使用'{$ FID}'時沒有使用字符串連接。 :) – hjpotter92

4

小錯字錯FID和組之間添加空格如下:

$q = 'select r_present,r_reminder 
from z_events 
where resident_id = ? 
    and r_added > date_sub(curdate(), interval 37 day) 
    and fascility_id='.$fid. ' 
group by r_added 
order by r_added 
desc limit 4'; 

和一個建議:沒必要用雙引號附上$ fid "

這裏我假設你的字段facility_id是整數類型。如果它的字符串,然後""加雙引號之如:

$q = 'select r_present,r_reminder 
from z_events 
where resident_id = ? 
    and r_added > date_sub(curdate(), interval 37 day) 
    and fascility_id="'.$fid. '" 
group by r_added 
order by r_added 
desc limit 4';