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

时间:2015-10-08 09:22:59

标签: php html mysql

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

<?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>

任何人都可以告诉我我需要做什么,因为我无法弄清楚要添加什么以及在哪里添加。 提前谢谢了, 丹尼

3 个答案:

答案 0 :(得分:1)

将以下代码放在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}?>

答案 1 :(得分:1)

你应该把你的HTML放在循环中

<?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>
<?php }
}
?>

答案 2 :(得分:0)

<?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");
    }
    ?>
    <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>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Email Address</th>
        <th>MobileNumber</th>
        <th>Work Phone</th>
        <th>Home Phone</th>
    </tr>
    <?
    while ($row = mysql_fetch_array($userquery, MYSQL_ASSOC))
    {
        $firstname = $row['forename'];
        $lastname = $row['surname'];
        $email = $row ['email'];
    ?>
    <tr>
        <td><?php echo strtoupper($firstname); ?></td>
        <td><?php echo strtoupper($lastname); ?></td>
        <td><?php echo strtoupper($email); ?></td>
        <td>#</td>
        <td>##</td>
        <td>###</td>
    </tr>
    <?php}?>
    </table>
<?}?>
相关问题