我需要數據庫驗證錯誤的幫助。驗證錯誤數據庫在codeigniter
我有一個模型:
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;
}
}
我有這樣的代碼在控制器:
$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;
}
}
參數$ id_student和$ id_course可以在數據庫中存在或沒有。我需要,如果查詢給出錯誤或者查詢不存在,跳過錯誤,執行$ total [$ i] = 0並且不顯示錯誤數據庫並繼續循環。我不知道我該怎麼做。我在論壇上嘗試過很多選項,但是我做不到。謝謝你的幫助。我很抱歉我的英語。
有(我希望)在您的代碼中的一些拼寫錯誤;從學生選擇總數id_student ='。$ id_student。 '和id_course ='。$ id_course和$ id_course = array(); – Rooneyl