Php 选择表中的多行

Php 选择表中的多行,php,mysql,Php,Mysql,我需要根据photo_id选择多个注释(如果有)。据我所知,您可以使用WHERE子句,但我不确定如何选择多个注释并将它们存储在某种数组中 e、 g $result = mysqli_query($conn,"SELECT * FROM comments WHERE photo_id='$photo1id'"); $row = $result->fetch_assoc(); // but there's more than 1 row 例如,如果$photo1id==21,如何获取所有注释

我需要根据photo_id选择多个注释(如果有)。据我所知,您可以使用WHERE子句,但我不确定如何选择多个注释并将它们存储在某种数组中

e、 g

$result = mysqli_query($conn,"SELECT * FROM comments WHERE photo_id='$photo1id'");
$row = $result->fetch_assoc(); // but there's more than 1 row
例如,如果
$photo1id==21
,如何获取所有注释(本例中为2)?某种while循环

在PHP文件的末尾,我有以下内容:

echo json_encode(array('photo1id'=>$photo1id));

我需要以某种方式存储该数组中的每一行,因为我需要使用$.getJSON检索另一个PHP文件中的数据。或者有更好的解决方案。

如果行数更多,则需要使用循环

while ($row = $result->fetch_assoc()) {
    // your code here
}
请尝试以下代码:

//Run query
$result = mysqli_query($conn,"SELECT * FROM comments WHERE photo_id='$photo1id'");

//While there is a result, fetch it
while($row = $result->fetch_assoc()) {
    //Do what you need to do with the comment
}
如果不想直接打印代码,可以创建一个数组:

$x=0;
while($row = $result->fetch_assoc()) {
    $comment[$x]=$row['comment'];
    $x++;
}

循环
并生成一个
数组
-

while($row = $result->fetch_assoc()) {
    $comments[] = $row;
}
之后,您可以将数组作为
json
发送

echo json_encode($comments);