我正在使用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-&gt ; db-> query($ sql)“这是两次发送到mySQL。 也许我在CodeIgniter中有一些错误的数据库配置,你们中的任何人都知道发生了什么吗?
答案 0 :(得分: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;
}
TO:
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”的部分无法正常工作,因此当我尝试INSERT时,“$ result_id-> execute()”被调用了两次。