0

我正在使用 CodeIgniter 进行开发,一段时间后我注意到所有查询都被执行了两次,如下面的 mySQL 日志所示:

           55 Query SELECT user_display FROM user WHERE user_id=1
       55 Query SELECT user_display FROM user WHERE user_id=1
       55 Query SELECT TIMEDIFF(NOW(),contest_create) FROM contest WHERE contest_id=95
       55 Query SELECT TIMEDIFF(NOW(),contest_create) FROM contest WHERE contest_id=95
       55 Query SELECT TIMEDIFF(ADDDATE(contest_create, INTERVAL contest_duration DAY),NOW()) FROM contest WHERE contest_id=95
       55 Query SELECT TIMEDIFF(ADDDATE(contest_create, INTERVAL contest_duration DAY),NOW()) FROM contest WHERE contest_id=95
       55 Query SELECT add_addon_id FROM addon_contest WHERE add_contest_id=95
       55 Query SELECT add_addon_id FROM addon_contest WHERE add_contest_id=95
       55 Query SELECT addon_name FROM addon WHERE addon_id='1' OR addon_id='4'
       55 Query SELECT addon_name FROM addon WHERE addon_id='1' OR addon_id='4'
       55 Query SELECT required_skill_id FROM skill_contest WHERE required_contest_id=95
       55 Query SELECT required_skill_id FROM skill_contest WHERE required_contest_id=95
       55 Query SELECT skill_name FROM skill WHERE skill_id='2' OR skill_id='4' OR skill_id='13'
       55 Query SELECT skill_name FROM skill WHERE skill_id='2' OR skill_id='4' OR skill_id='13'
       55 Query SELECT user_display FROM user WHERE user_id=2
       55 Query SELECT user_display FROM user WHERE user_id=2
       55 Query SELECT TIMEDIFF(NOW(),contest_create) FROM contest WHERE contest_id=9
       55 Query SELECT TIMEDIFF(NOW(),contest_create) FROM contest WHERE contest_id=9
       55 Query SELECT TIMEDIFF(ADDDATE(contest_create, INTERVAL contest_duration DAY),NOW()) FROM contest WHERE contest_id=9
       55 Query SELECT TIMEDIFF(ADDDATE(contest_create, INTERVAL contest_duration DAY),NOW()) FROM contest WHERE contest_id=9
       55 Query SELECT add_addon_id FROM addon_contest WHERE add_contest_id=9
       55 Query SELECT add_addon_id FROM addon_contest WHERE add_contest_id=9
       55 Query SELECT addon_name FROM addon WHERE addon_id='1'
       55 Query SELECT addon_name FROM addon WHERE addon_id='1'
       55 Query SELECT required_skill_id FROM skill_contest WHERE required_contest_id=9
       55 Query SELECT required_skill_id FROM skill_contest WHERE required_contest_id=9
       55 Query SELECT skill_name FROM skill WHERE skill_id='1' OR skill_id='4'
       55 Query SELECT skill_name FROM skill WHERE skill_id='1' OR skill_id='4'
       55 Query SELECT user_display FROM user WHERE user_id=2
       55 Query SELECT user_display FROM user WHERE user_id=2
       55 Query SELECT TIMEDIFF(NOW(),contest_create) FROM contest WHERE contest_id=16
       55 Query SELECT TIMEDIFF(NOW(),contest_create) FROM contest WHERE contest_id=16
       55 Query SELECT TIMEDIFF(ADDDATE(contest_create, INTERVAL contest_duration DAY),NOW()) FROM contest WHERE contest_id=16
       55 Query SELECT TIMEDIFF(ADDDATE(contest_create, INTERVAL contest_duration DAY),NOW()) FROM contest WHERE contest_id=16
       55 Query SELECT add_addon_id FROM addon_contest WHERE add_contest_id=16
       55 Query SELECT add_addon_id FROM addon_contest WHERE add_contest_id=16
       55 Query SELECT addon_name FROM addon WHERE addon_id='2' OR addon_id='4'
       55 Query SELECT addon_name FROM addon WHERE addon_id='2' OR addon_id='4'
       55 Query SELECT required_skill_id FROM skill_contest WHERE required_contest_id=16
       55 Query SELECT required_skill_id FROM skill_contest WHERE required_contest_id=16
       55 Query SELECT skill_name FROM skill WHERE skill_id='16'
       55 Query SELECT skill_name FROM skill WHERE skill_id='16'
       55 Query SELECT user_display FROM user WHERE user_id=1
       55 Query SELECT user_display FROM user WHERE user_id=1
       55 Query SELECT TIMEDIFF(NOW(),contest_create) FROM contest WHERE contest_id=68
       55 Query SELECT TIMEDIFF(NOW(),contest_create) FROM contest WHERE contest_id=68
       55 Query SELECT TIMEDIFF(ADDDATE(contest_create, INTERVAL contest_duration DAY),NOW()) FROM contest WHERE contest_id=68
       55 Query SELECT TIMEDIFF(ADDDATE(contest_create, INTERVAL contest_duration DAY),NOW()) FROM contest WHERE contest_id=68
       55 Query SELECT add_addon_id FROM addon_contest WHERE add_contest_id=68
       55 Query SELECT add_addon_id FROM addon_contest WHERE add_contest_id=68
       55 Query SELECT addon_name FROM addon WHERE addon_id='7'
       55 Query SELECT addon_name FROM addon WHERE addon_id='7'
       55 Query SELECT required_skill_id FROM skill_contest WHERE required_contest_id=68
       55 Query SELECT required_skill_id FROM skill_contest WHERE required_contest_id=68
       55 Query SELECT skill_name FROM skill WHERE skill_id='1' OR skill_id='3'
       55 Query SELECT skill_name FROM skill WHERE skill_id='1' OR skill_id='3'
       55 Query SELECT user_display FROM user WHERE user_id=1
       55 Query SELECT user_display FROM user WHERE user_id=1
       55 Query SELECT TIMEDIFF(NOW(),contest_create) FROM contest WHERE contest_id=79
       55 Query SELECT TIMEDIFF(NOW(),contest_create) FROM contest WHERE contest_id=79
       55 Query SELECT TIMEDIFF(ADDDATE(contest_create, INTERVAL contest_duration DAY),NOW()) FROM contest WHERE contest_id=79
       55 Query SELECT TIMEDIFF(ADDDATE(contest_create, INTERVAL contest_duration DAY),NOW()) FROM contest WHERE contest_id=79
       55 Query SELECT add_addon_id FROM addon_contest WHERE add_contest_id=79
       55 Query SELECT add_addon_id FROM addon_contest WHERE add_contest_id=79
       55 Query SELECT addon_name FROM addon WHERE addon_id='4'
       55 Query SELECT addon_name FROM addon WHERE addon_id='4'
       55 Query SELECT required_skill_id FROM skill_contest WHERE required_contest_id=79
       55 Query SELECT required_skill_id FROM skill_contest WHERE required_contest_id=79
       55 Query SELECT skill_name FROM skill WHERE skill_id='2' OR skill_id='4' OR skill_id='6'
       55 Query SELECT skill_name FROM skill WHERE skill_id='2' OR skill_id='4' OR skill_id='6'
       55 Query SELECT user_display FROM user WHERE user_id=1
       55 Query SELECT user_display FROM user WHERE user_id=1
       55 Query SELECT TIMEDIFF(NOW(),contest_create) FROM contest WHERE contest_id=86
       55 Query SELECT TIMEDIFF(NOW(),contest_create) FROM contest WHERE contest_id=86
       55 Query SELECT TIMEDIFF(ADDDATE(contest_create, INTERVAL contest_duration DAY),NOW()) FROM contest WHERE contest_id=86
       55 Query SELECT TIMEDIFF(ADDDATE(contest_create, INTERVAL contest_duration DAY),NOW()) FROM contest WHERE contest_id=86
       55 Query SELECT add_addon_id FROM addon_contest WHERE add_contest_id=86
       55 Query SELECT add_addon_id FROM addon_contest WHERE add_contest_id=86
       55 Query SELECT addon_name FROM addon WHERE addon_id='2' OR addon_id='3'
       55 Query SELECT addon_name FROM addon WHERE addon_id='2' OR addon_id='3'
       55 Query SELECT required_skill_id FROM skill_contest WHERE required_contest_id=86
       55 Query SELECT required_skill_id FROM skill_contest WHERE required_contest_id=86
       55 Query SELECT skill_name FROM skill WHERE skill_id='8' OR skill_id='13'
       55 Query SELECT skill_name FROM skill WHERE skill_id='8' OR skill_id='13'
       55 Query SELECT user_display FROM user WHERE user_id=1
       55 Query SELECT user_display FROM user WHERE user_id=1
       55 Query SELECT TIMEDIFF(NOW(),contest_create) FROM contest WHERE contest_id=92
       55 Query SELECT TIMEDIFF(NOW(),contest_create) FROM contest WHERE contest_id=92
       55 Query SELECT TIMEDIFF(ADDDATE(contest_create, INTERVAL contest_duration DAY),NOW()) FROM contest WHERE contest_id=92
       55 Query SELECT TIMEDIFF(ADDDATE(contest_create, INTERVAL contest_duration DAY),NOW()) FROM contest WHERE contest_id=92
       55 Query SELECT add_addon_id FROM addon_contest WHERE add_contest_id=92
       55 Query SELECT add_addon_id FROM addon_contest WHERE add_contest_id=92
       55 Query SELECT addon_name FROM addon WHERE addon_id='3' OR addon_id='4' OR addon_id='5' OR addon_id='8'
       55 Query SELECT addon_name FROM addon WHERE addon_id='3' OR addon_id='4' OR addon_id='5' OR addon_id='8'
       55 Query SELECT required_skill_id FROM skill_contest WHERE required_contest_id=92
       55 Query SELECT required_skill_id FROM skill_contest WHERE required_contest_id=92
       55 Query SELECT skill_name FROM skill WHERE skill_id='3' OR skill_id='6' OR skill_id='19'
       55 Query SELECT skill_name FROM skill WHERE skill_id='3' OR skill_id='6' OR skill_id='19'
       55 Query SELECT user_display FROM user WHERE user_id=2
       55 Query SELECT user_display FROM user WHERE user_id=2
       55 Query SELECT TIMEDIFF(NOW(),contest_create) FROM contest WHERE contest_id=12
       55 Query SELECT TIMEDIFF(NOW(),contest_create) FROM contest WHERE contest_id=12
       55 Query SELECT TIMEDIFF(ADDDATE(contest_create, INTERVAL contest_duration DAY),NOW()) FROM contest WHERE contest_id=12
       55 Query SELECT TIMEDIFF(ADDDATE(contest_create, INTERVAL contest_duration DAY),NOW()) FROM contest WHERE contest_id=12
       55 Query SELECT add_addon_id FROM addon_contest WHERE add_contest_id=12
       55 Query SELECT add_addon_id FROM addon_contest WHERE add_contest_id=12
       55 Query SELECT addon_name FROM addon WHERE addon_id='3' OR addon_id='4' OR addon_id='8'
       55 Query SELECT addon_name FROM addon WHERE addon_id='3' OR addon_id='4' OR addon_id='8'
       55 Query SELECT required_skill_id FROM skill_contest WHERE required_contest_id=12
       55 Query SELECT required_skill_id FROM skill_contest WHERE required_contest_id=12
       55 Query SELECT skill_name FROM skill WHERE skill_id='11'
       55 Query SELECT skill_name FROM skill WHERE skill_id='11'
       55 Query SELECT user_display FROM user WHERE user_id=1
       55 Query SELECT user_display FROM user WHERE user_id=1
       55 Query SELECT TIMEDIFF(NOW(),contest_create) FROM contest WHERE contest_id=34
       55 Query SELECT TIMEDIFF(NOW(),contest_create) FROM contest WHERE contest_id=34
       55 Query SELECT TIMEDIFF(ADDDATE(contest_create, INTERVAL contest_duration DAY),NOW()) FROM contest WHERE contest_id=34
       55 Query SELECT TIMEDIFF(ADDDATE(contest_create, INTERVAL contest_duration DAY),NOW()) FROM contest WHERE contest_id=34
       55 Query SELECT add_addon_id FROM addon_contest WHERE add_contest_id=34
       55 Query SELECT add_addon_id FROM addon_contest WHERE add_contest_id=34
       55 Query SELECT add_addon_id FROM addon_contest WHERE add_contest_id=34
       55 Query SELECT required_skill_id FROM skill_contest WHERE required_contest_id=34
       55 Query SELECT required_skill_id FROM skill_contest WHERE required_contest_id=34
       55 Query SELECT skill_name FROM skill WHERE skill_id='3'
       55 Query SELECT skill_name FROM skill WHERE skill_id='3'
       55 Query SELECT user_display FROM user WHERE user_id=1
       55 Query SELECT user_display FROM user WHERE user_id=1
       55 Query SELECT TIMEDIFF(NOW(),contest_create) FROM contest WHERE contest_id=74
       55 Query SELECT TIMEDIFF(NOW(),contest_create) FROM contest WHERE contest_id=74
       55 Query SELECT TIMEDIFF(ADDDATE(contest_create, INTERVAL contest_duration DAY),NOW()) FROM contest WHERE contest_id=74
       55 Query SELECT TIMEDIFF(ADDDATE(contest_create, INTERVAL contest_duration DAY),NOW()) FROM contest WHERE contest_id=74
       55 Query SELECT add_addon_id FROM addon_contest WHERE add_contest_id=74
       55 Query SELECT add_addon_id FROM addon_contest WHERE add_contest_id=74
       55 Query SELECT addon_name FROM addon WHERE addon_id='1'
       55 Query SELECT addon_name FROM addon WHERE addon_id='1'
       55 Query SELECT required_skill_id FROM skill_contest WHERE required_contest_id=74
       55 Query SELECT required_skill_id FROM skill_contest WHERE required_contest_id=74
       55 Query SELECT skill_name FROM skill WHERE skill_id='2'
       55 Query SELECT skill_name FROM skill WHERE skill_id='2'
       55 Query SELECT skill_name FROM skill WHERE skill_status=2
       55 Query SELECT skill_name FROM skill WHERE skill_status=2
       55 Quit  

问题是当一些 INSERT 查询运行时,所有信息都被插入两次,查看查询的顺序我可以得出结论,我没有调用模型两次,但是当我调用 "$this->db-> query($sql)" 这被发送到 mySQL 两次。也许我在 CodeIgniter 中有一些错误的数据库配置,你们中有人知道发生了什么吗?

4

1 回答 1

0

好吧,我发现codeigniter系统内部CI_DB_pdo_driver中的_execute()函数没有正常工作,可能是php版本的问题。无论如何,我将 CodeIgniter 的代码从:

    function _execute($sql)
{
    $sql = $this->_prep_query($sql);
    $result_id = $this->conn_id->prepare($sql);
    $result_id->execute();

    if (is_object($result_id))
    {
        if (is_numeric(stripos($sql, 'SELECT')))
        {
            $this->affect_rows = count($result_id->fetchAll());
            $result_id->execute();
        }
        else
        {
            $this->affect_rows = $result_id->rowCount();
        }
    }
    else
    {
        $this->affect_rows = 0;
    }

    return $result_id;
}

到:

    function _execute($sql)
{
    $sql = $this->_prep_query($sql);
    $result_id = $this->conn_id->prepare($sql);
    $result_id->execute();

    if (is_object($result_id))
    {
        if (preg_match('/^\s*"?(SELECT)\s+/i', $sql))
        {
            $this->affect_rows = count($result_id->fetchAll());
            $result_id->execute();
        }
        else
        {
            $this->affect_rows = $result_id->rowCount();
        }
    }
    else
    {
        $this->affect_rows = 0;
    }

    return $result_id;
}

测试 $sql 内部是否包含“SELECT”的部分不起作用,因此当我尝试插入时,“$result_id->execute()”被调用了两次。

于 2013-11-04T15:21:03.360 回答