CodeIgniter - 两个左连接导致连接重置

时间:2012-09-11 17:53:40

标签: php codeigniter

交叉发布在codeigniter论坛上,我得到了蟋蟀。

我有一个查询,我使用了两个左连接和几个内连接,它始终在firefox中返回连接重置错误,并且在apache日志中没有错误消息。

如果我删除'left'参数,则不会发生相同的错误,如果我直接在数据库中添加LEFT连接,则从$ this-> db-> last_query()运行SQL,它会返回正确(在phpMyAdmin或命令行中)。

不需要传递“内部”参数,我将它们放在那里进行实验。

$this->db->select('votes.uid AS voterUID, comments.uid AS voteeUID, voteTypes.id AS voteTypeID, userratingVoter.level AS voterLevel, userratingVotee.level AS voteeLevel');

$this->db->from('votes');

# Join the comments table on comments.id = votes.cid
$this->db->join('comments', 'comments.id = votes.cid', 'inner');

# Join the userauth table for admin WHERE clause below
$this->db->join('userauth',  'userauth.uid = votes.uid', 'inner');

# Join the votesTypes table for the WHERE IN clause below
$this->db->join('voteTypes', 'voteTypes.id = votes.voteid', 'inner');

# Join the userrating table twice once for the Voter once for the Votee
$this->db->join('userrating AS userratingVotee', 'userratingVotee.uid = comments.uid', 'left');
$this->db->join('userrating AS userratingVoter', 'userratingVoter.uid = votes.uid', 'left');

# Where in valid Vote Types
# Valid Vote Types: helpful, interesting, correct, incorrect
#                   5        4            2        7
$validVoteTypes = array(5,4,2,7);
$this->db->where_in('votes.voteid', $validVoteTypes);

# Where vote is active.
$this->db->where('votes.active',1);

# Where timestamp is greater than the last run timestamp.
$this->db->where('votes.timestamp >',$lastrun);

# Only standard user votes count, may have to update this if we get other authids
$this->db->where('userauth.authid',2);

从$ this-> db-> last_query()中分解SQL ...在phpMyAdmin和命令行中运行。

SELECT `votes`.`uid` AS voterUID, `comments`.`uid` AS voteeUID, `voteTypes`.`id` AS voteTypeID, `userratingVoter`.`level` AS voterLevel, `userratingVotee`.`level` AS voteeLevel

FROM (`votes`)

INNER JOIN `comments` ON `comments`.`id` = `votes`.`cid`
INNER JOIN `userauth` ON `userauth`.`uid` = `votes`.`uid`
INNER JOIN `voteTypes` ON `voteTypes`.`id` = `votes`.`voteid`

LEFT JOIN `userrating` AS userratingVotee ON `userratingVotee`.`uid` = `comments`.`uid`
LEFT JOIN `userrating` AS userratingVoter ON `userratingVoter`.`uid` = `votes`.`uid`

WHERE `votes`.`voteid` IN (5, 4, 2, 7)
 AND `votes`.`active` = 1
 AND `votes`.`timestamp` > '2012-03-01 00:00:00'
 AND `userauth`.`authid` = 2

此外,如果我使用标准查询而不是活动记录语法,则会产生相同的结果。

$sql = "SELECT `votes`.`uid` AS voterUID, `comments`.`uid` AS voteeUID, `voteTypes`.`id` AS voteTypeID, `userratingVoter`.`level` AS voterLevel, `userratingVotee`.`level` AS voteeLevel FROM (`votes`) INNER JOIN `comments` ON `comments`.`id` = `votes`.`cid` INNER JOIN `userauth` ON `userauth`.`uid` = `votes`.`uid` INNER JOIN `voteTypes` ON `voteTypes`.`id` = `votes`.`voteid` LEFT JOIN `userrating` AS userratingVotee ON `userratingVotee`.`uid` = `comments`.`uid` LEFT JOIN `userrating` AS userratingVoter ON `userratingVoter`.`uid` = `votes`.`uid` WHERE `votes`.`voteid` IN (5, 4, 2, 7) AND `votes`.`active` = 1 AND `votes`.`timestamp` > '2012-03-01 00:00:00' AND `userauth`.`authid` = 2";

$query = $this->db->query($sql);

如果我使用标准JOIN而不是LEFT JOIN,则不会发生错误,但它不会返回我需要的数据。

服务器信息:

Apache / 2.2.14(Ubuntu) PHP / 5.3.2 MySQL 5.1.63 CodeIgniter 2.1.2

应用/配置/ database.php中

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'some_user';
$db['default']['password'] = 'some_pass';
$db['default']['database'] = 'some_db';
$db['default']['dbdriver'] = 'mysqli';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;  

1 个答案:

答案 0 :(得分:1)

你在Firefox中看到错误的原因可能是因为PHP出于某种原因异常退出。 如果您可以在Web服务器上找到错误日志,您可能会看到原因。编辑:刚发现您已查看错误日志;也许PHP没有设置为登录?

最可能的原因是查询返回的数据太多,以至于PHP耗尽内存或处理时间试图处理所有内容。如果是这种情况,您可能希望尝试在查询中使用LIMITOFFSET子句来一次获取数据的“页面”。