[英]How do I display the same MySQL query twice using PHP?
I selected values from my table, and wanted to display them two separate times. 我从表中选择了值,并希望分别显示两次。 In the first instance, I wanted to display all cells in a table, and this works. 首先,我想显示表格中的所有单元格,并且可以正常工作。 In the second instance, I wanted to make a drop down from one of the columns. 在第二种情况下,我想从其中一列进行下拉。 Only the first one works; 只有第一个有效; how can I get both to work? 我怎样才能同时工作? I also need all of the rows in the table to display, which is why I thought a while loop was a good idea. 我还需要显示表中的所有行,这就是为什么我认为while循环是个好主意的原因。 But please correct me if I'm wrong in my thinking. 但是,如果我的想法有误,请纠正我。 Thanks. 谢谢。
<!DOCTYPE html>
<?php
$host = "localhost";
$username = "redacted";
$password = "redacted";
$db = "redacted";
$connection = mysqli_connect($host, $username, $password, $db);
if (mysqli_connect_errno()) {
echo "Connection error";
}
$query = "SELECT * FROM main";
$result = $connection->query($query);
?>
<html>
<head>
<title>Test Page</title>
</head>
<body>
<h1>Test Main Table Display</h1>
<table>
<?php
// first instance
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['user'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['count'] . "</td>";
echo "<td>" . $row['action'] . "</td>";
echo "<td><button>Expand</button></td>";
echo "</tr>";
}
?>
</table>
<h1>Test User Entry Appendment</h1>
<form>
<label>Select a user, or add a new user.</label><br>
<select>
<?php
// second instance
while($row = mysqli_fetch_array($result)) {
echo "<option>" . $row['user'] . "</option>";
}
?>
</select>
</form>
</body>
</html>
add this line before second loop set the pointer back to the beginning 在第二个循环之前添加此行, 将指针设置回开头
mysqli_data_seek($result, 0); mysqli_data_seek($ result,0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.