Welcome Guest, Not a member yet? Register   Sign In
Insert Queries - Checking the query has been carried out
#1

[eluser]the future darlo manager[/eluser]
Starting to get my head around CodeIgniter now. The user guide is pretty good and I've bought myself a book. So far I am preferring it ahead of CakePHP as for legacy databases it seems to be a lot less hassle.

Anyway, a quick question about insets. In my old way of doing things with php I would write something like the following (the query isn't really important - the bit underneath is):

Code:
//Query to add the record into the history table    
$add_history_query = "INSERT INTO cacdp_content_history (content_id, date, comment) VALUES ('$insert_id', NOW(), 'Created by $user_name')";
//Runs the query
$add_history_result = @mysql_query ($add_history_query);

//Checks the query ran okay
if ($add_history_result) {
echo "Query ran okay and record was inserted.";
} else {
echo "Something went wrong. Query Record was not inserted.";
}

Basically I got into a habit of checking if the query was ran okay and echoing out a certain message. I just want to know if the following is the right way to do it in the framework.

Controller
Code:
$data['page_title'] = "TEST INSERT PAGE";
$this->load->model('Test_model', '', TRUE);
$data['insert_user_query'] = $this->Test_model->insertUser($username, $passconf, $email);
            
if ($data['insert_user_query']) {
echo "Query was a success";
//I know I would load a view here with a success but for the purposes of this...
} else {
echo "Query failed";
//I know I would load a view here with a failure but for the purposes of this...
}

Model
Code:
class Test_model extends Model{
function Test_model(){
parent::Model();
}
    
function insertUser($username, $passconf, $email){
$query = $this->db->query("INSERT INTO people (username, password, email)
VALUES ('$username', '$passconf', '$email')");
return $query;
}

}

Sorry for asking but I'm just trying to get my knowledge of standard php up to the same level in the framework.
#2

[eluser]xwero[/eluser]
I believe the query method returns a boolean so your example is one way of checking if the query is executed but a better way is to return
Code:
$this->db->affected_rows()
#3

[eluser]sikkle[/eluser]
If you use id key, look at $this->db->insert_id() too.
#4

[eluser]mattpointblank[/eluser]
I have an issue with this - using $this->db->affected_rows() doesn't return anything if the data being updated is the same as the existing data (eg, if a user accidentally clicks the 'update details' button without changing any of the stored information I have on them) - this returns an error message in my app. Is there a way to check that a query returned an actual error, as opposed to affected rows?

Cheers
Matt
#5

[eluser]ok3x[/eluser]
Code:
function update($data){
  // update statement..
  return $this->db->affected_rows() ? true : false;
}
If the values ($data) pass the controller (where you should check/validate it), then this model's function returns whether the query succeeded or not - if false, the data passed didn't change (assuming you performed validity check and possibly returned an error message from the controller).

So there may be two types of error messages: 1) from controller (the data passed is invalid) and 2) from model (the data passed is valid but didn't change)




Theme © iAndrew 2016 - Forum software by © MyBB