Database handling in Bonfire


Inserting data to table
$other_det_data = array(  
'id' => 2,  
'color_id' => 2,  
'name' => 'test'  
);  
$insert_other_data = $this->db->insert($this->db->dbprefix.'user_details', $other_det_data);  
Where keys in array are the column names of the database table "user_details"  
Select a number of rows from tables
$query = $this->db->query("select
                                  users.*,
                                  user_profiles.user_type,
                                  account.account_name from
                                  {$this->db->dbprefix}users users,
                                  {$this->db->dbprefix}user_profiles user_profiles,
                                  {$this->db->dbprefix}account account
                                   where users.id = user_profiles.user_id
                                   and account.account_id = user_profiles.account_id ");
$results = $query->result();                      
foreach($results as $row)
{
echo $row->user_type;
}
You can also use the result_array() to get values as arrays instead of objects.
$results = $query->result_array();                      
foreach($results as $row)
{
echo $row[‘user_type’];
}
Select one row from a table
$query = $this->db->query("YOUR QUERY");

   $row = $query->row();

   echo $row->title;
   echo $row->name;
   echo $row->body;

you can also use the row_array() to get values as array instead of objects.
$query = $this->db->query("YOUR QUERY");

   $row = $query->row_array();

   echo $row['title'];
   echo $row['name'];
   echo $row['body'];

Get number of rows returned by query
$query = $this->db->query('SELECT * FROM my_table');

echo $query->num_rows();

Get number of fields returned by query
$query = $this->db->query('SELECT * FROM my_table');

echo $query->num_fields();

Get last incremented id
$user_id = $this->db->insert_id();