2016-10-20 164 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>"; 
     } 
    }