返回多行是具有相同id的PHP Query

时间:2014-04-22 21:47:52

标签: php sql-server-2008 ssms

我试图让页面显示与该页面相关的所有评论。但是它只返回列表中的第一条评论。

$commentIdQuery = "SELECT commentid FROM COMMENT_RECIPE WHERE RecipeID = '$recipeID[0]'";
$CID = mssql_query($commentIdQuery, $connection);
$commentID = mssql_fetch_row($CID);
$commentQuery = "SELECT UserID, Rating, Comment FROM COMMENTS WHERE CommentID = '$commentID[0]'";

然后打印它我

    while($row = mssql_fetch_row($commentQuery)){   
echo "<td> $row[0] $row[1] $row[2]<br><br></td>";           
}   

1 个答案:

答案 0 :(得分:0)

尝试JOINING两个查询 -

$query = "SELECT COMMENTS.UserId, COMMENT.Rating, COMMENT.Comment,
          COMMENT_RECIPE.commentid
          FROM COMMENT INNER JOIN COMMENT_RECIPE ON
          COMMENT.CommentID = COMMENT_RECIPE.commentid
          WHERE COMMENT_RECIPE.RecipeID = '$recipeID[0]'
         "; 
$execute = mssql_query($query, $connection);

echo "<table>";
echo "<tr>
      <th>User Id</th>
      <th>Rating</th>
      <th>Comments</th>
      </tr>";
while($row = mssql_fetch_row($execute))
{
     echo "<tr>
      <td>$row[0]</td>
      <td>$row[1]</td>
      <td>$row[2]</td>
      </tr>";         
}
echo "</table>";