0

我不确定为什么我仍然得到答案,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);
}
4

1 回答 1

2

您的模型函数没有返回任何内容,因此当您从控制器 ( $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,因为您没有检索值(即 noresult_array()results()),所以您总是会返回一个对象(DB 类)。

更新

重新阅读您的问题,看起来您想检查是否存在某些东西,仅此而已,因此您应该执行以下操作:

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);
}
于 2013-10-23T05:10:40.553 回答