sark9012
sark9012

Reputation: 5747

How to extract database information from query?

I can't seem to find the documentation laying this out clearly.

public function get_user_id_from_username($username){

    $this->db->where('username', $username);
    $this->db->select('id');
    $query = $this->db->get('users');

}

Quite simply, I want to return the id that I have just selected from the database. Can anyway tell me how I get access to that id?

Thanks.

Upvotes: 0

Views: 51

Answers (2)

Michael Benjamin
Michael Benjamin

Reputation: 2915

public function get_user_id_from_username($username){
   $query='select id from users where username=?';
   $params=array();
   $params[]=$username;

   $result=$this->db->query($query, $params);
   $result=$result->row_array();
   return $result['id'];
}

if you're expecting multiple rows result_array works:

$result=$this->db->query($query, $params);
$result=$result->result_array();

//uses the form $result[0]['id'] 
//try a print_r
print_r($result);

Upvotes: 1

John Conde
John Conde

Reputation: 219864

The manual was very enlightening

$row = $query->row();
echo $row->id;

Upvotes: 1

Related Questions