0

NULLDBに存在するのに、なぜまだ答えが得られるのかわかりません。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']);
}

コントローラ:

$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メソッドに a を追加するだけです:

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()値を取得していないため (つまり、 noまたは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 に答える