简体   繁体   中英

How can I output multiple results from a database (if there are any)?

I have made a search function which outputs peoples names and their details after searching the first name, but when there are multiple people with the same first name in the database the output only shows one result. Here is my code:

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

Can anyone tell me what I need to do here as I can't figure out what to add and where. Many thanks in advance, Danny

Put below peice of code in while loop :

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

You should put your HTML inside loop

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM