[英]How do I display data from one mySQL table into multiple tables in php?
I have on mySQL table with data and I want to display it in multiple tables. 我在带有数据的mySQL表上,并且想要在多个表中显示它。 When I try to do it with the code I have now, the data doesn't show up in the second table. 当我尝试使用现在的代码进行操作时,数据不会显示在第二张表中。 Is my code wrong? 我的代码错了吗?
$id=$_GET['id'];
$sql = "SELECT * FROM buildings WHERE room = '{$id}' AND number = '81'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<center><h2>Room $id</h2></center> <br> <b>General Information<br>
<table><tr>
<th>Room Type</th>
<th>Department</th>
<th>College</th>
<th>Primary Owner</th></tr>";
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>".$row["type"]."</td>
<td>".$row["Department"]."</td>
<td>".$row["College"]."</td>
<td>".$row["Primary"]."</td>
</tr>";
}
echo "</table>";
}
//CONTACT INFO//
if (mysqli_num_rows($result) > 0) {
echo "<p><b>Contact Information<br><table><tr>
<th>Reservations</th>
<th>CTL Contact Name</th>
<th>CTL Contact Email</th>
<th>CTL Contact Phone</th>
<th>Department Contact Name</th>
<th>Department Contact Email</th>
<th>Department Contact Phone</th>
<th>IT Contact Name</th>
<th>IT Contact Email</th>
<th>IT Contact Phone</th>
</tr>";
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>".$row["reservations"]."</td>
<td>".$row["contact"]."</td>
<td>".$row["contactphone"]."</td>
<td>".$row["deptcontact"]."</td>
<td>".$row["deptcontactemail"]."</td>
<td>".$row["deptcontactphone"]."</td>
<td>".$row["itcontact"]."</td>
<td>".$row["itcontactemail"]."</td>
<td>".$row["itcontactphone"]."</td>
</tr>";
}
echo "</table>";
}
else{
echo("0 results");
}
This is what I get: http://i57.tinypic.com/2heijyc.png 这就是我得到的: http : //i57.tinypic.com/2heijyc.png
Read the manual of mysqli_fetch_assoc 阅读mysqli_fetch_assoc的手册
You've already called while($row = mysqli_fetch_assoc($result))
您已经调用了while($row = mysqli_fetch_assoc($result))
This will loop through the entire result array and get each row. 这将遍历整个结果数组并获得每一行。 mysqli_fetch_assoc
returns null when there are no more rows. 当没有更多行时, mysqli_fetch_assoc
返回null。 So the next time you run mysqli_fetch_assoc($result)
it will be null. 因此,下次您运行mysqli_fetch_assoc($result)
,它将为null。
What you need to do is use mysqli_fetch_all at the top to get an array of the result set. 您需要做的是在顶部使用mysqli_fetch_all获取结果集的数组。 Then you can loop through this array as needed. 然后,您可以根据需要遍历此数组。
As I stated in my comment, this query is subject to SQL injections because you use $id
which is sourced by $_GET
directly in the query. 正如我在评论中所述,此查询需要进行SQL注入,因为您使用直接由$_GET
来源的$_GET
$id
。 Read up on prepared statements or if it is an integer, use intval($id)
. 阅读准备好的语句,或者如果它是整数,请使用intval($id)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.