2013-07-07 64 views
-1

我需要從new_guest表得到guest_id其中柱guest_nic_pp_dl是等於變量guest_nic_pp_dlLIMIT到1個結果的查詢。然而,我寫的代碼以一個愚蠢的語法錯誤結束。我如何定義WHERE與變量值匹配行?

Parse error: syntax error, unexpected '$guest_nic_pp_dl' (T_VARIABLE) in C:\xampp\htdocs\bit\application\models\reservations_model.php on line 20

$query = $this->db->query('SELECT guest_id FROM new_guest WHERE guest_nic_pp_dl, '$guest_nic_pp_dl' LIMIT 1'); // Line 20 
foreach ($query->result() as $row) { 
    return $row->guest_id; 
+1

WHERE guest_nic_pp_dl = '$ guest_nic_pp_dl' – sinisake

+0

還是同樣的錯誤:( –

回答

2

嘗試這樣

 $query = $this->db->query("SELECT guest_id FROM new_guest WHERE guest_nic_pp_dl, '$guest_nic_pp_dl' LIMIT 1"); 
+0

感謝。有效! –

+1

歡迎您:)! –

2

$this->db->select('guest_id');

$this->db->where('guest_nic_pp_dl', $guest_nic_pp_dl);

$this->db->limit(1);

$query = $this->db->get('new_guest');

+0

個人而言,我不喜歡這種方式,但如果有必要,我會記住這一點。謝謝! :) –

+1

我建議儘可能挖掘CI活動記錄..對於簡單的查詢可能看起來很明顯,但使生活更容易,DB獨立..此外,它會自動轉義您的變量... – blowdoof

2
$query = $this->db->query('SELECT guest_id FROM new_guest WHERE guest_nic_pp_dl='.$guest_nic_pp_dl.' LIMIT 1'); 
foreach ($query->result() as $row) { 
return $row->guest_id; 
}