3
votes

Is it safe to assume in codeigniter query builder class for every successful query it will automatically return true and false if it fails?

If so, does it apply to CRUD? or is it only true when inserting and deleting data? For updating would i have to check affected rows? or will it also return a true result if update was successful?

For example:

controller:

$data = array('username' => $this->input->post('username'),
    'password' => password_hash($this->input->post('password'), PASSWORD_BCRYPT),
    'date_created' => mdate('%Y-%m-%d', time())
);

$result = $this->account_model->create($data);

if($result == TRUE){
    $this->session->set_flashdata('message', 'Registration Successful');
    redirect('pages/login');
}else{
    $this->session->set_flashdata('message', 'Registration Failed');
    redirect('pages/register');
}

model:

public function create($new_user){
    $insert = array('username' => $new_user['username'],
        'password' => $new_user['password'], 
        'date_created' => $new_user['date_created']
    );

    $result = $this->db->insert('user', $insert);

    return $result;

}

also, is it bad if i returned the query statement itself? like,

return $this->db->insert('user', $insert);
1
The Codeigniter Docu is pretty straight forward - Just take a look at e.g. codeigniter.com/user_guide/database/… - an insert statement always returns true or false the same applies for update - its probably up to you how you assess those specifications. - sintakonte
I tried to output the result of a query and it returned an object array. Maybe because in php if($var), will be true if var isn't empty. It also doesnt specifically says in the userguide that a successful query returns true and false if not. i tried looking for it with ctrl+f but i just couldnt find it. - Awing

1 Answers

2
votes

No. Not really. You can, however, control what the model returns. Keep in mind that an empty resultset, for example, is not necessarily a "failed" query. There's a lot of use cases where an empty resultset may be a good thing.

After actually running the query you will get an object with the result

for instance, when selecting, you can do $result = $this->db->get() and your $result variable will become an object with a lot of information you can access:

Number of returned rows: $result->num_rows(); Returned contents: $result->result(); Specific rows: $result->row(0); Specific field in a specific row: $result->row(3)->field_name;

When inserting or updating, $this->db->affected_rows() becomes available so you can check how many rows were updated or inserted. Inserts also make $this->db->insert_id() available in case you need the table's primary key value for the row you inserted.

Thus, you are in control of the logic.

when selecting, I usually do:

$query = $this->db->get();
if ($query->num_rows() == 0)
{
  return false;
}

else
{
  return $query->row(0);
}

Then I check, in the controller, if the model returned false or a real resultset (sometimes I just return true instead of the actual resultset if I'm just checking for something to exist). You can tailor the results to your needs.

When inserting, I usually check:

if ($this->db->affected_rows() != 0)
{
  return $this->db->insert_id();
}

else
{
  return false;
}

(in the controller, a false being returned would be handled as an "insertion failed").

To answer your question more globally: No, nothing really happens by default, but Codeigniter gives you everything you need to tailor the behavior of your site to your specific needs and tastes