簡體   English   中英

如何從數據庫輸出多個結果(如果有)?

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

我已經創建了一個搜索功能,可以在搜索姓氏后輸出人名及其詳細信息,但是當數據庫中有多個具有相同名字的人時,輸出僅顯示一個結果。 這是我的代碼:

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

您應該將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 }
}
?>
<?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>
<?}?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM