2012-09-11 56 views
0

Cross發佈了codeigniter論壇,我在那裏得到了蟋蟀。CodeIgninter - 連接重置中的兩個左連接結果

我有一個查詢,我使用兩個左連接和幾個內部連接,它始終返回一個連接重置錯誤在Firefox和沒有錯誤消息在Apache日誌。

如果我刪除了'left'參數,同樣的錯誤不會發生,如果我直接在數據庫中運行SQL從$ this-> db-> last_query(),然後添加LEFT加入,它會正確地返回(在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的63年5月1日 笨2.1.2

的application/config/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

您在Firefox中看到錯誤的原因大概是因爲PHP出於某種原因異常退出。 如果您可以在Web服務器上找到錯誤日誌,您可能會看到原因。編輯:剛纔發現你已經在錯誤日誌中看過;也許PHP沒有設置在那裏登錄?

最有可能的原因是查詢返回了大量的數據,PHP將耗盡內存或嘗試處理全部內容的處理時間。如果是這種情況,您可能需要嘗試在查詢中使用LIMITOFFSET子句以一次獲取數據的「頁面」。

+0

我將配置中的log_threshold更改爲4「All Messages」,並檢查了.htaccess中的任何異常情況。在apache的日誌或CI特定的日誌中仍然沒有提供任何有用的東西。 當我直接從命令行運行查詢時,得到49個結果。我添加了限制1並仍然出錯。 – paulj