2011-01-25 13 views
0

から単一の行を取得し、:CodeIgniterの

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

があります必要なものがすべて1つのデータであれば、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); 
+0

コードは機能しますか? –

+0

いいえ、非オブジェクトのプロパティを取得しようとしているというエラーが表示されます。 – sehummel

+0

「メンバー」テーブルに必要な行が存在していますか? –

答えて

2

$query->row()->idが正しいです。フィールドがあなたのテーブルで 'id'と呼ばれ、あなたが望む行が存在すると仮定すると。 $query->num_rows()をチェックすると、返された行の数を確認できます。

0
$the_data= $query->row(); 

$user_id= $the_data->id; 

はあなたの例のように連鎖する方法を行うことができるとは思いません。

+1

はい、チェーンを使用できます。 – jondavidjohn

+0

私は訂正しました! – Ross

0
$this->db->select(array('tbl_airport.id')); 
      $this->db->from(array('tbl_airport')); 
      $this->db->where('tbl_airport.flag', 0); 
      $this->db->where('tbl_airport.airport_code', $input['destination']); 
      $query = $this->db->get(); 
      $destinatin_id = $query->row()->id;