I need help with validation error in database.
I have a model :
function total ($id_student, $id_course){
$query = $this->db->query('select total from student where id_student='.$id_student. ' and $id_course='.$id_course);
if ($query->num_rows() <= 0) {
return false;
}
if ($query->num_rows() > 0) {
$row = $query->row();
return $row->total;
}
}
I have this code in the controller:
$id_course=array;
$total = array();
for ($i = 0; $i < count($list_courses); $i++) {
$total[$i] = $this->student_model->total($id_student, $id_course[$i]);
$error[$i]= $this->db->_error_message();
if(!empty($error[$i])){
$total[$i] = 0;
}
}
The parameters $id_student and $id_course can exist in the database or no. I need that if the query gives error or the query not exists, skip the error, do $total[$i]=0 and don’t show the error database and continue the loop. I don’t know how can I do this. I have tried many options in the forums, but I couldn’t. Thanks for your help. I’m sorry my english.
In your model method, you’re returning false if the query doesn’t produce a result. So you can do this: