2012-01-17 142 views
0

我無法找到我的函數沒有得到所有結果的原因。當我轉到頁面查看評論時,我只能看到最令人憎惡的一個。如果我然後刪除該評論,則顯示下一個最新評論。我確定這件事很簡單,我沒有注意到。

function getComments($inPostID=null) 
{ 
    $commentArray = array(); 

    if (!empty($inPostID)) 
    { 
     //echo " Get comments for the post with the postID of ". $inPostID; 
     $stmt = db::connect()->prepare("SELECT * FROM Comments WHERE postID = ? ORDER BY commentDate DESC"); 
     $stmt->bind_param('i', $inPostID); 
     $stmt->execute(); 
     $stmt->bind_result($commentID, $postID, $userID, $commentDate, $commentContent); 
     while($stmt->fetch()) 
     { 
      echo"HI "; 
      $thisComment = new ViewComment($commentID, $postID, $userID, $commentDate, $commentContent); 
      array_push($commentArray, $thisComment); 
     } 
     $stmt->close(); 
    } 

    return $commentArray; 
} 
+0

「HI」會回覆多少次?你確定那個特定的postID有多個評論嗎? – 2012-01-17 05:07:02

+0

你檢查了什麼'$ stmt-> execute'產生了嗎? – Sterex 2012-01-17 05:09:08

+0

只有一次,我把它放在那裏進行測試。是的,我敢肯定。我在數據庫上檢查了它,當我刪除了最新的帖子評論時,它提出了該帖子的下一個最新評論。 – 2012-01-17 05:09:57

回答

4

我已經想通了。我打開一個綁定語句,然後在ViewComment()函數中打開另一個綁定語句,以從其他表中獲得更多信息。解決的辦法是將bindResults存儲到一個將由while填充的數組中,然後關閉該綁定語句。然後循環執行for循環中的結果量,該循環調用ViewComment(),pramitors是bindResults數組中的數組。

代碼如下。

function getComments($inPostID=null) 
{ 
    $commentArray = array(); 
    $tempArray = array(); 

    if (!empty($inPostID)) 
    { 
     //echo " Get comments for the post with the postID of ". $inPostID; 
     $stmt = db::connect()->prepare("SELECT * FROM Comments WHERE postID = ? ORDER BY commentDate DESC"); 
     $stmt->bind_param('i', $inPostID); 
     $stmt->execute(); 
     $stmt->bind_result($commentID, $postID, $userID, $commentDate, $commentContent); 
     while($stmt->fetch()) 
     { 
      $bindResults = array($commentID, $postID, $userID, $commentDate, $commentContent); 
      array_push($tempArray, $bindResults); 
     } 
     $stmt->close(); 
     $total = count($tempArray); 
     for($i = 0; $i < $total; $i++) 
     { 
      $thisComment = new ViewComment($tempArray[$i][0], $tempArray[$i][1], $tempArray[$i][2], $tempArray[$i][3], $tempArray[$i][4]); 
      array_push($commentArray, $thisComment); 
     } 
    } 
    return $commentArray; 
} 
0

取()將獲取只有一個記錄,獲取所有記錄使用使用fetchall(),改變像

while($stmt->fetchAll()) 
+1

致命錯誤:調用未定義的方法mysqli_stmt :: fetchAll() – 2012-01-17 05:16:42

+0

您可以在while語句之前放置print_r($ stmt)並檢查結果集 – 2012-01-17 05:21:45

+0

mysqli_stmt Object([affected_rows] => -1 [insert_id] => 0 [num_rows] => 0 [param_count] => 1 [field_count] => 5 [errno] => 0 [error] => [sqlstate] => 00000 [id] => 1) – 2012-01-17 05:31:04

0

請變更 array_push($ commentArray,$ thisComment)代碼; 到

foreach($ thisComment as $ k => $ v) array_push($ commentArray,$ v);

+0

這沒有奏效,謝謝你的建議。任何其他想法? – 2012-01-17 05:31:33

+0

還有其他想法嗎? – 2012-01-17 05:46:00

+0

我已經修復了它,並會在StackOverflow允許我在一小時內發佈答案。請評價它,讓我的代表上升,所以我不會在將來等待。謝謝你們的幫助! – 2012-01-17 11:12:24

0

我想你應該有使用以下

$stmt->bind_param(1, $inPostID); 

代替

$stmt->bind_param('i', $inPostID); 
+0

不需要,它需要'我'來表明它是一個我綁定的整數。 – 2012-01-17 07:51:31

+0

你試過嗎?或者打印/轉儲sql語句? – renghen 2012-01-17 07:54:14

+0

我嘗試過,我想嘗試任何東西。來自var_dump($ stmt)的輸出; (0)[「num_rows」] => int(0)[「param_count」)之前執行的對象(mysqli_stmt)#2(9){[「affected_rows」] => int 「] => int(1)[」field_count「] => int(5)[」errno「] => int(0)[」error「] => string(0)」「[」sqlstate「] => string(5)「00000」[「id」] => int(1)}'放置在object(mysqli_stmt)#2(9){[「affected_rows」] => int(-1)[「insert_id」]之後=> int(0)[「num_rows」] => int(0)[「param_count」] => int(1)[「field_count」] => int(5)[「errno」] => int(0) [「error」] => string(0)「」[「sqlstate」] => string(5)「00000」[「id」] => int(1)}' – 2012-01-17 08:14:01