gpt4 book ai didi

php - 如何在 PHP 代码中使用 Select 显示多列

转载 作者:行者123 更新时间:2023-11-29 13:31:25 26 4
gpt4 key购买 nike

所以我写了这段代码 -

$result = mysqli_query($con,"SELECT * FROM Appointments");

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Address</th>
<th>City</th>
<th>PIN</th>
<th>Mobile</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "<td>" . $row['City'] . "</td>";
echo "<td>" . $row['PIN'] . "</td>";
echo "<td>" . $row['Mobile'] . "</td>";
echo "</tr>";
}
echo "</table>";

但是如果我想执行 -

SELECT FirstName, Address FROM Appointments WHERE LastName='Something'

我怎样才能做到这一点?如何只显示两列?我可以通过更改文件中的表来手动完成此操作。

但是我们如何根据请求的查询更改表呢?因此,如果请求两列,则只会显示两列。

最佳答案

$result = mysqli_query($con,"SELECT FirstName, Address FROM Appointments WHERE LastName='Something'");

echo "<table border='1'>";
while($row = mysqli_fetch_row($result)) {
echo "<tr><td>$row[0]</td><td>$row[1]</td></tr>"; // index 0 being FirstName and index 1 being address.

}
echo "</table>";

关于php - 如何在 PHP 代码中使用 Select 显示多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19411415/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com