2012-03-10 147 views
0

我在分頁時遇到了一些問題。我直接在我的數據庫控制檯上執行查詢,並且工作正常。使用PDO和MySQL分頁

public function method($arg, $db)//$db is a PDO connection link 
{ 
    try 
    { 
     $next = $arg * 9; 
     $top = 9; 
     $sql = "SELECT col01, col02, col03 "; 
     $sql .= "FROM table "; 
     $sql .= "ORDER BY col01 ASC "; 
     $sql .= ($next === 0)? "LIMIT ".$top : "LIMIT ".$next.", ".$top;  
     $return = $db->prepare($sql); 
     $return->execute(); 

     $return->setFetchMode(PDO::FETCH_ASSOC); 
     $this->minis = $return->fetch(); 
     return true; 
    } 
    catch(PDOExcepction $e) 
    { 
     return false; 
    } 
} 

我在做什麼錯?

+0

當你在最後一個連接後回顯$ sql時......打印什麼? – 2012-03-10 02:34:00

+0

這是我測試查詢得到結果所做的第一件事情:SELECT col01,col02,col03 FROM table ORDER BY col01 ASC LIMIT 9(將其編輯爲示例的值;使用$ arg = 0進行測試) – matt 2012-03-10 02:39:04

+0

事實上,我複製了腳本創建的確切查詢並將其粘貼到mysql控制檯並執行它。但是當在瀏覽器上測試時只顯示第一行 – matt 2012-03-10 02:41:10

回答

2

您只返回第一行,因爲您只撥打fetch()一次。說它是一個循環,結果累積到一個數組:

while ($row = $return->fetch()) { 
    // Append the current row onto your array 
    $this->minis[] = $row; 
} 
return true; 
+1

FetchAll pdo中不需要while循環。 – itachi 2012-03-10 07:23:51

+0

是的,但是給定的數組有時候比必要的數值更多 – matt 2012-03-10 17:00:54

1
$this->minis = $return->fetchAll(); 

it'l在一多維數組的形式返回所有數據。