从Codeigniter检索单行

时间:2011-01-25 22:22:08

标签: php codeigniter

如果我需要从数据库中检索单行数据,当我想将该ID插入另一个表时,为什么我不能这样做:

'user_id' => $query->row()->id

如果没有foreach循环,如果我想要的只是一个数据,有没有办法做到这一点?

完整的代码是:

$this->db->where('username', $this->input->post('username'));
        $query = $this->db->get('members');

        $data = array(
            'first' => $this->input->post('first'),
            'last' => $this->input->post('last'),
            'email' => $this->input->post('email'),
            'phone' => $this->input->post('phone'),
            'address' => $this->input->post('address'),
            'city' => $this->input->post('city'),
            'state' => $this->input->post('state'),
            'zip' => $this->input->post('zip'),
            'comments' => $this->input->post('comments'),
            'username' => $this->input->post('username'),
            'user_id' => $query->row()->id
        );

        $this->db->insert('contact', $data);

3 个答案:

答案 0 :(得分:2)

$query->row()->id是正确的,是的。鉴于该字段被称为“id'在您的表中,该行存在您想要的。您可以检查$query->num_rows()以查看返回的行数。

答案 1 :(得分:0)

$the_data= $query->row();

$user_id= $the_data->id;

不认为你可以像你的例子那样进行方法链接。

答案 2 :(得分:0)

sqlite3
相关问题