2013-04-24 175 views
0

這對我來說有點頭疼,因爲我一直在使用PDO而沒有任何問題。但是今天我有這個代碼的問題:PDO FetchALL只返回一行

$query = "SELECT exit_time, exit_url FROM exit_log ORDER BY exit_time ASC LIMIT 50"; 
    $stmt = $db->query($query, PDO::FETCH_ASSOC); 
    $posts = $stmt->fetchAll(); 
    print_r($posts); 

的問題是不是與查詢本身因爲phpMyAdmin的查詢結果在50行,我需要。但是,當我運行上面的代碼我得到這個,只是這樣的:

Array ([0] => Array ([exit_time] => 1366714175 [exit_url] => http://blogbaladi.com/trillium-strikes-again/) 

我幾乎可以發誓這是我使用的爲工作得很好,其他項目相同的代碼。

+0

您確定表中有多於一行的行嗎? – Svetoslav 2013-04-24 11:41:40

+1

您確定您正在使用您的PHP代碼訪問正確的數據庫服務器嗎?您可能正在查看PhpMyAdmin中的生產數據庫,並使用您的腳本獲取開發數據庫的行... – 2013-04-24 11:45:31

+0

您是一位天才Miklos!把它作爲答覆,所以我可以接受它.. – Mustapha 2013-04-24 11:48:58

回答

2

你確定你正在用你的PHP代碼打正確的數據庫服務器嗎?您可能正在查看PhpMyAdmin中的生產數據庫並使用您的腳本獲取開發數據庫的行...

0

太長註釋:這是什麼

$query = "SELECT Count(*) as cnt FROM exit_log"; 
$stmt = $db->query($query, PDO::FETCH_ASSOC); 
echo __FILE__, '@', __LINE__, "\r\n"; 
print_r($stmt->fetchAll()); 


$query = "SELECT exit_time, exit_url FROM exit_log ORDER BY exit_time ASC LIMIT 50"; 
$stmt = $db->query($query, PDO::FETCH_ASSOC); 
$posts = $stmt->fetchAll(); 
echo __FILE__, '@', __LINE__, "\r\n"; 
print_r($posts); 

打印?

+0

謝謝VolkerK,我認爲Miklos釘在上面的評論http://stackoverflow.com/questions/16191012/pdo-fetchall-returning-just-one-row#comment23147469_16191012 ,我用我的php代碼打錯了服務器.. – Mustapha 2013-04-24 11:52:03