CodeIgniter为什么在删除查询中添加额外的“where”?

时间:2014-03-05 05:35:09

标签: mysql codeigniter where

由于某种原因,CodeIgniter在我的删除查询中添加了一个额外的“where”子句。很明显MySQL错误发生的原因......但为什么CI会添加这个额外的条款?

这是错误:

Unknown column '8' in 'where clause'
DELETE FROM `forum_messages` WHERE `thread_id` = '8' AND `8` IS NULL

这是我的代码:

function delete($thread_id){

    $this->db->save_queries = FALSE;

    $this->db->where('thread_id', $thread_id);
    if(!$this->db->delete('forum_messages')){

        $this->error = "The messages in this thread could not be deleted because of a database error: ".$this->db->_error_message();

    } else {

        $this->db->where('thread_id', $thread_id);
        if(!$this->db->delete('forum_threads')){

            $this->error = "The thread could not be deleted because of a database error: ".$this->db->_error_message();

        }

    }


}

更新:

以下建议的答案均无效。出于某种原因,我必须在forum_threads之前删除之前切换删除查询并从forum_messages中删除。

2 个答案:

答案 0 :(得分:0)

else条件中有一个额外的where条件。现在检查一下。

  function delete($thread_id){

            $this->db->save_queries = FALSE;

            $this->db->where('thread_id', $thread_id);
            if(!$this->db->delete('forum_threads')){

                $this->error = "The thread could not be deleted because of a database error: ".$this->db->_error_message();

            } else {
                if(!$this->db->delete('forum_messages')){

                    $this->error = "The messages in this thread could not be deleted because of a database error: ".$this->db->_error_message();

                }

            }

        }

答案 1 :(得分:0)

else语句中有一个额外的$this->db->where('thread_id',$thread_id)

试试这个:

function delete($thread_id){
        $this->db->save_queries = FALSE;
        $this->db->where('thread_id', $thread_id);
        if(!$this->db->delete('forum_threads')){
            $this->error = "The thread could not be deleted because of a database error: ".$this->db->_error_message();
        } else {
            if(!$this->db->delete('forum_messages')){
                $this->error = "The messages in this thread could not be deleted because of a database error: ".$this->db->_error_message();
            }
        }
    }