0

我有以下代码:

$getrand = "SELECT DISTINCT Name from Events Order BY Rand() Limit 1";
$result = mysqli_query($con,$getrand);
$data = mysqli_fetch_array($result);
echo "<div class = 'container'>Name: $data[0]</br>";
$gettags = "SELECT Tag From Events where Name = '$data[0]'";
$tagresult = mysqli_query($con, $gettags);
$tagdata = mysqli_fetch_array($tagresult);
echo "$tagdata[0], $tagdata[1]";

但是它只返回一个值tagdata。我的数据库看起来像:

身份证 | 姓名 | 标签

一个名字可以有多个标签(即每个名字有多行标签)

4

1 回答 1

1

您需要像这样遍历您的记录集...

$getrand = "SELECT DISTINCT Name from Events Order BY Rand() Limit 1";
$result = mysqli_query($con,$getrand);
$data = mysqli_fetch_array($result);
echo "<div class = 'container'>Name: $data[0]</br>";
$gettags = "SELECT Tag From Events where Name = '$data[0]'";
$tagresult = mysqli_query($con, $gettags);

while($tagdata = mysqli_fetch_array($tagresult)) // a while loop has been used here
{
echo $tagdata['Tag'];// 'tag' your column name (change it as per your requirement)
}
于 2013-11-09T18:47:47.490 回答