如果 "no result found" 或没有 "input was entered",则 Codeigniter 错误;其他工作正常
Codeigniter error if "no result found" or no "input was entered"; Else works fine
我正处于学习阶段,正在尝试开发这个搜索结果应用程序。
如果找到结果,一切正常,但如果没有输入或没有结果,它会显示这两个错误;
Message: Undefined offset: 0
Message: Trying to get property of non-object
这是错误发生的地方search_model.php
function get_city_id_by_input($keyword){
$this->db->select('id');
$this->db->from('vbc_city');
$this->db->where('v_city_name', $keyword);
$query = $this->db->get();
$query_result = $query->result();
$row = $query_result[0];
return $row->id;
}
和控制器:
$keyword = $this->input->post('search[1]');
$city_id = $this->search_model->get_city_id_by_input($keyword);
$data['results'] = $this->search_model->get_search_results($city_id);
$this->load->view('search', $data);
这样试试
模型中
function get_city_id_by_input($keyword){
$this->db->select('id');
$this->db->from('vbc_city');
$this->db->where('v_city_name', $keyword);
$query = $this->db->get();
$query_result = $query->result_array(); # Changed
if (empty($query_result)) {
return FALSE;
}
elseif (count($query_result) > 1) {
return 0;
}
else{
$rowId = $query_result[0]['id']; # Changed
return $rowId; # Changed
}
}
在控制器中
$keyword = $this->input->post('search[1]');
if (empty($keyword)) {
echo "Input is Empty";
}
else{
$city_id = $this->search_model->get_city_id_by_input($keyword);
$result = $this->search_model->get_search_results($city_id);
if ($result == FALSE) {
echo "No Data Found";
}
elseif ($result == 0) {
echo "Multiple of records founds";
}
else{
$data['results'] = $result
$this->load->view('search', $data);
}
}
我正处于学习阶段,正在尝试开发这个搜索结果应用程序。 如果找到结果,一切正常,但如果没有输入或没有结果,它会显示这两个错误;
Message: Undefined offset: 0
Message: Trying to get property of non-object
这是错误发生的地方search_model.php
function get_city_id_by_input($keyword){
$this->db->select('id');
$this->db->from('vbc_city');
$this->db->where('v_city_name', $keyword);
$query = $this->db->get();
$query_result = $query->result();
$row = $query_result[0];
return $row->id;
}
和控制器:
$keyword = $this->input->post('search[1]');
$city_id = $this->search_model->get_city_id_by_input($keyword);
$data['results'] = $this->search_model->get_search_results($city_id);
$this->load->view('search', $data);
这样试试
模型中
function get_city_id_by_input($keyword){
$this->db->select('id');
$this->db->from('vbc_city');
$this->db->where('v_city_name', $keyword);
$query = $this->db->get();
$query_result = $query->result_array(); # Changed
if (empty($query_result)) {
return FALSE;
}
elseif (count($query_result) > 1) {
return 0;
}
else{
$rowId = $query_result[0]['id']; # Changed
return $rowId; # Changed
}
}
在控制器中
$keyword = $this->input->post('search[1]');
if (empty($keyword)) {
echo "Input is Empty";
}
else{
$city_id = $this->search_model->get_city_id_by_input($keyword);
$result = $this->search_model->get_search_results($city_id);
if ($result == FALSE) {
echo "No Data Found";
}
elseif ($result == 0) {
echo "Multiple of records founds";
}
else{
$data['results'] = $result
$this->load->view('search', $data);
}
}