您可以在每次循环循环时检查名称,只有在新循环时才输出
<?php include ("dbconnect.php"); $sql="SELECT post.postID, post.postTitle, post.postDesc, post.postImage FROM post JOIN category ON (post.categoryID=category.categoryID) WHERE post.categoryID=?"; $stmt = $con->prepare($sql); $stmt->bind_param('i', $_GET['categoryID']); $stmt->execute(); $result = $stmt->get_result(); $last_name = ''; while ( $row = $result->fetch_assoc() ) { if ( $last_name != $row['postTitle'] ) { echo $row['postTitle']; $last_name = $row['postTitle']; } echo $row['postDesc']; } ?>