首先,让我告诉你,我是 PHP 和 MySQL 的新手,并且正在努力学习。因此,我创建了一个 web 应用程序,其中将根据会员编号资历排序的人员姓名显示随机公寓编号。公寓号码和人员姓名来自不同的表格。通过实现这样的查询,我能够获得一个随机名称-
SELECT bd, rank, name FROM person ORDER BY RAND() DESC LIMIT 1
但我需要的是 - 当我提交表格时,它会显示最高级的人的姓名和随机的公寓号码,当我再次点击提交表格时,下一个老人和随机的公寓号码等等。在那种情况下,我的查询应该是什么?我的代码看起来像这样-
if (isset($_POST['submit'])) {
$query = "SELECT house_no FROM houses ORDER BY RAND() LIMIT 1";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result))
{
global $won;
$won = $row['house_no'];
echo $won;
}
$query2 = "SELECT bd, rank, name FROM person ORDER BY RAND() DESC LIMIT 1";
$result2 = mysqli_query($link, $query2);
while ($row2 = mysqli_fetch_array($result2))
{
$winner_rank = $row2['rank'];
$winner = $row2['name'];
$winner_bd = $row2['bd'];
global $winner, $winner_bd, $winner_rank;
echo $row2['rank']. " ";
echo $row2['name']. ", ";
echo "Member No/".$row2['bd'];
} }