2016-10-20 106 views
-3

我遇到SELECT查詢的問題。該查詢僅輸出一個結果,而不是此數據庫中的所有行。SELECT查詢只輸出一個結果

有沒有人看到爲什麼?

$select_comments_sql = " 
    SELECT * 
    FROM home_comments 
    ORDER BY id DESC 
"; 
    if ($select_comments_stmt = $con->prepare($select_comments_sql)) { 
     //$select_comments_stmt->bind_param("s", $user_id); 
     $select_comments_stmt->execute(); 
     if (!$select_comments_stmt->errno) { 
      //echo "error"; 
     } 
     $select_comments_stmt->bind_result($comment_id, $comment_user_id, $comment_username, $home_comments, $comment_date); 

     $comment_array = array(); 
     while ($select_comments_stmt->fetch()) { 
      $comment_array[] = $comment_user_id; 
      $comment_array[] = $comment_username; 
      $comment_array[] = $home_comments; 
      $comment_array[] = $comment_date; 
     } 
     if ($home_comments === NULL) { 
      echo 'No comments found.'; 
     } else { 
      echo $comment_username. "<br>"; 
      echo $home_comments. "<br><br><br>"; 
     } 
    } 

AJAX

$("#comment-form").on("submit", function (event) { 
    event.preventDefault(); 

    var home_comment = $("#home_comment").val(); 

    $.ajax({ 
     url: "ajax-php/comment-send.php", 
     type: "POST", 
     data: { 
      "home_comment": home_comment 
     }, 
     success: function (data) { 
     // console.log(data); // data object will return the response when status code is 200 
      if (data == "Error!") { 
       alert("Unable to post comment!"); 
       alert(data); 
      } else { 
       $("#comment-form")[0].reset(); 
       //$('.newsletter-popup').fadeIn(350).delay(2000).fadeOut(); 
      } 
     }, 
     error: function (xhr, textStatus, errorThrown) { 
      alert(textStatus + " | " + errorThrown); 
      console.log("error"); //otherwise error if status code is other than 200. 
     } 
    }); 
}); 

PHP

$user = new User(); 

    $home_comment = $_POST['home_comment']; 
    $username = $user->data()->username; 
    $okay = true; 

    if ($okay) { 

     $comment_insert = " 
      INSERT INTO home_comments 
      (id, user_id, username, comment, date) 
      VALUES(?, ?, ?, ?, NOW()) 
      "; 
     $comment_stmt = $con->prepare($comment_insert); 
     $comment_stmt->bind_param('ssss', $id, $user_id, $username, $home_comment); 
     $comment_stmt->execute(); 
     } 
+0

請檢查您的while循環,並分配到陣列是否正確。好像它正在替換值,只有最後一個值應該在那裏 – Aruna

+0

由於你在循環外回顯結果,所以你只會得到最後的結果。 –

回答

1

你輸出,同時循環之外的結果。由於您將整個結果集存儲在$comment_array[]中,因此您可以轉儲它以獲取從DB獲取的所有評論,或者將其輸出到循環中。

if ($select_comments_stmt = $con->prepare($select_comments_sql)) { 
    //$select_comments_stmt->bind_param("s", $user_id); 
    $select_comments_stmt->execute(); 
    if (!$select_comments_stmt->errno) { 
     //echo "error"; 
    } 
    $select_comments_stmt->bind_result($comment_id, $comment_user_id, $comment_username, $home_comments, $comment_date); 

    $comment_array = array(); 
    while ($select_comments_stmt->fetch()) { 
     $comment_array[] = $comment_user_id; 
     $comment_array[] = $comment_username; 
     $comment_array[] = $home_comments; 
     $comment_array[] = $comment_date; 
     if ($home_comments === NULL) { 
      echo 'No comments found.'; 
     } else { 
      echo $comment_username. "<br>"; 
      echo $home_comments. "<br><br><br>"; 
     } 
    } 
    // Alternatively: print_r($comment_array); 
} 
+0

謝謝!快速提問。此選擇查詢用於頁面加載以顯示評論。但是,我有一個Ajax調用,在提交時插入註釋,我可以在Ajax中添加同樣的選擇查詢,在插入之下使其在提交後立即顯示新評論(無頁面加載),或者我必須做些什麼其他? – Paul

+1

運行整個查詢並重新獲得整個結果集並不是一個好主意。您可以編寫ajax請求來僅提取新評論。 –

+0

@Paul推遲內容加載是一種很好的做法。您還應該對通話中提取的評論數量設置限制。我也建議與上面評論的蘇哈爾相同的東西。 – hjpotter92

1

你寫的代碼有幾個問題。首先,您將在$comment_array陣列中追加每列的值。您應該在while循環中創建一個子數組,並在$comment_array中創建多維數組。

其次,您只是顯示$comment_username的值,它將是while循環的最後一個用戶名。

您應該遍歷$comment_array並分別顯示每個用戶名和註釋。

代碼看起來這一點,

$comment_array = array(); 
    while ($select_comments_stmt->fetch()) { 
     $sub_array=array(); 
     $sub_array["userid"] = $comment_user_id; 
     $sub_array["username"] = $comment_username; 
     $sub_array["comments"] = $home_comments; 
     $sub_array["date"] = $comment_date; 
     $comment_array[]=$sub_array; 
    } 
    if ($home_comments === NULL) { 
     echo 'No comments found.'; 
    } else { 
     echo $comment_username. "<br>"; 
     echo $home_comments. "<br><br><br>"; 

     // Loop over comments array. 

     foreach($comment_array as $comment) { 
      echo $comment["username"]. "<br>"; 
      echo $comment["comments"]. "<br><br><br>"; 
     } 
    }