gpt4 book ai didi

php - 如何从数据库中输出多个结果(如果有的话)?

转载 作者:行者123 更新时间:2023-11-29 06:21:41 25 4
gpt4 key购买 nike

我做了一个搜索功能,在搜索名字后输出人名和他们的详细信息,但是当数据库中有多个名字相同的人时,输出只显示一个结果。这是我的代码:

<?php
include ('connect-db.php');
if (isset($_GET ['forename'])){
$forename = $_GET['forename'];


$userquery = mysql_query("SELECT * FROM staff WHERE forename = '$forename'") or die ("error getting information from database.");
if (mysql_num_rows($userquery) == null ){
die ("No staff directory found");
}
while ($row = mysql_fetch_array($userquery, MYSQL_ASSOC)){
$firstname = $row['forename'];
$lastname = $row['surname'];
$email = $row ['email'];
}
}
?>

<h2><?php echo strtoupper( $forename )?> <?php echo strtoupper ( $lastname )?>'s Profile</h2><br><br><br>
<table style = "font-weight:bold;
font-family: Century Gothic,CenturyGothic,AppleGothic,sans-serif;">
<hr style="border-top: dotted 1px;" />
<tr><td>First Name: &nbsp &nbsp</td><td><?php echo strtoupper ($firstname) ?> </td></tr>
<tr><td>Last Name: </td><td><?php echo strtoupper ($lastname) ?> </td></tr>
<tr><td>Email Address: </td><td><?php echo strtoupper ($email) ?> </td></tr>
<tr><td>Mobile Number: </td><td> #</td></tr>
<tr><td>Work Number: </td><td> ##</td></tr>
<tr><td>Home Phone: </td><td> ###</td></tr>
</table>

任何人都可以告诉我我需要在这里做什么,因为我不知道要添加什么以及在哪里添加。提前谢谢了,丹尼

最佳答案

将以下代码放入 while 循环中:

<?php


while ($row = mysql_fetch_array($userquery, MYSQL_ASSOC)){
$firstname = $row['forename'];
$lastname = $row['surname'];
$email = $row ['email'];

?>


<h2><?php echo strtoupper( $forename )?> <?php echo strtoupper ( $lastname )?>'s Profile</h2><br><br><br>
<table style = "font-weight:bold;
font-family: Century Gothic,CenturyGothic,AppleGothic,sans-serif;">
<hr style="border-top: dotted 1px;" />
<tr><td>First Name: &nbsp &nbsp</td><td><?php echo strtoupper ($firstname) ?> </td></tr>

<tr><td>Last Name: </td><td><?php echo strtoupper ($lastname) ?> </td></tr>

<tr><td>Email Address: </td><td><?php echo strtoupper ($email) ?> </td></tr>
<tr><td>Mobile Number: </td><td> #</td></tr>
<tr><td>Work Number: </td><td> ##</td></tr>
<tr><td>Home Phone: </td><td> ###</td></tr>
</table>

<?php}?>

关于php - 如何从数据库中输出多个结果(如果有的话)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33011645/

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