我不确定为什么即使数据库中确实存在NULL
,我仍然得到答案。 我也为数据库中没有的值获取它,应该插入它。
型:
function pageURLCheck($title, $id)
{
$this->db->select('post_title, website_id');
$this->db->where('post_title', $title);
$this->db->where('website_id', $id);
$query = $this->db->get($this->_table['posts']);
}
控制器:
$urlCheck = $this->page_model->pageURLCheck($post['post_title'], $website_id);
if($urlCheck == NULL)
{
$this->session->set_flashdata('flash', 'Page Title Exists', 'error');
}else{
$save_success = $this->page_model->save($post, $action, $post_id);
}
你的模型函数没有返回任何东西,所以当你从控制器($urlCheck = $this->page_model->pageURLCheck($post['post_title'], $website_id);
)调用它时,你会得到NULL
。
只需向该方法添加一个return
:
function pageURLCheck($title, $id)
{
$this->db->select('post_title, website_id');
$this->db->where('post_title', $title);
$this->db->where('website_id', $id);
return $this->db->get($this->_table['posts']);
}
另外,不要在控制器中检查 NULL,因为您没有检索值(即没有result_array()
或results()
),所以你总是得到一个对象(数据库类)。
更新
重新阅读您的问题,看起来您想检查是否存在某些东西,仅此而已,所以您应该执行以下操作:
function pageURLCheck($title, $id)
{
$this->db->select('post_title, website_id');
$this->db->where('post_title', $title);
$this->db->where('website_id', $id);
$query = $this->db->get($this->_table['posts']);
return $query->num_rows(); // <-- return the number of rows found in resultset
}
控制器:
$urlCheck = $this->page_model->pageURLCheck($post['post_title'], $website_id);
if($urlCheck > 0){
$this->session->set_flashdata('flash', 'Page Title Exists', 'error');
}
else{
$save_success = $this->page_model->save($post, $action, $post_id);
}