CodeIgniter:使用WHERE查询返回错误:: $ query = $ this-> db-> query()

时间:2013-07-07 20:09:35

标签: php mysql sql codeigniter

我需要从guest_id表中获取new_guest,其中列guest_nic_pp_dl等于变量guest_nic_pp_dlLIMIT为1。但是,我写的代码以愚蠢的语法错误结束。如何使用变量值定义WHERE以匹配行?

  

解析错误:语法错误,第20行的C:\ xampp \ htdocs \ bit \ application \ models \ reservations_model.php中的意外'$ guest_nic_pp_dl'(T_VARIABLE)

$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;

3 个答案:

答案 0 :(得分:2)

尝试就好了

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

答案 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');

答案 2 :(得分: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;
}