我有这个sql代码,我知道我只有一个结果,但查询结果是一个数组。如何获取字符串或int值?
$MEMBER_ID = $this->db
->select ('MEMBER_ID', false)
->from($this->table_member)
->where('MEMBER_EMAIL, $this->input->post('InputEmail'))
->get()
->result();
使用CodeIgniter。
谢谢,
答案 0 :(得分:0)
如果您确切知道该查询只获得一个结果,那么从第一个数组元素中取字段MEMBER_ID
:
$MEMBER_ID = $this->db
->select ('MEMBER_ID', false)
->from($this->table_member)
->where('MEMBER_EMAIL, $this->input->post('InputEmail'))
->get()
->result()[0]['MEMBER_ID'];
或者像这样使用更简单的查询:
$MEMBER_ID = $this->db->query("Select MEMBER_ID from table_member where id = $id")->row()->MEMBER_ID;
答案 1 :(得分:0)
对于这样一个简单的查询,您不需要所有这些Query Builder调用。这样可以正常工作。
$id = $this->input->post('InputEmail');
$MEMBER_ID = $this->db
->query("Select MEMBER_ID from {$this->table_member} where id = $id")
->row()
->MEMBER_ID;
但如果你坚持不懈的话
$MEMBER_ID = $this->db
->select ('MEMBER_ID', false)
->from($this->table_member)
->where('MEMBER_EMAIL, $this->input->post('InputEmail'))
->get()
->row()
->MEMBER_ID;
答案 2 :(得分:0)
如果您知道查询应该只返回1个结果,那么请不要使用ressult()
或result_array()
检查一下: Generating Query Results
所以在你的代码上你可以试试这个
{p}在row()
例外:
$MEMBER_ID = $this->db
->select ('MEMBER_ID', false)
->from($this->table_member)
->where('MEMBER_EMAIL, $this->input->post('InputEmail'))
->get()
->row()->MEMBER_ID;
return $MEMBER_ID;
row_array()
中的
$MEMBER_ID = $this->db
->select ('MEMBER_ID', false)
->from($this->table_member)
->where('MEMBER_EMAIL, $this->input->post('InputEmail'))
->get()
->row_array()['MEMBER_ID'];
return $MEMBER_ID;