数据未显示在数据库中

时间:2014-07-03 18:03:31

标签: php

我是PHP新手并尝试显示数据库中的数据。

但是它只显示一行中的数据,但我想显示条件匹配的多行数据。这是我正在使用的代码:

<?PHP


session_start();

if (!(isset($_SESSION['username']) && $_SESSION['username'] != '')) {

header ("Location: checklogin.php");
}

$con = mysqli_connect("localhost", "root", "", "map_my_way");
$fetch_row = $_SESSION['username'];

$result = mysqli_query($con,"SELECT * FROM members where username='$fetch_row'");

while($row = mysqli_fetch_array($result)) {
    $first_name = $row['first_name'];
    $last_name = $row['last_name'];
    $email = $row['email'];
    $m_no = $row['m_no'];
    $v_name = $row['v_name'];
    $capacity = $row['capacity'];
    $fuel_type = $row['fuel_type'];

}

$result2 = mysqli_query($con,"SELECT s_a_name FROM locations where  username='$fetch_row'");

$row2 = mysqli_fetch_array($result2);


?>

<!DOCTYPE HTML> 
<html> 
    <head> 
        <title>Profile Page</title> 
        <link rel="stylesheet" type="text/css" href="profile.css">
    </head> 
        <body id="body">
        <div id="mmw"> <span> MAP MY WAY </span></div>
        <div id="title_box">
                <a href="profile.php"><button id="lvbutton">My Profile</button></a>
                <a href="user_index.php"><button id="lvbutton">Maps</button></a>
                <a href="edit.php"><button id="lvbutton">Edit Profile</button></a>
                <a href="logout.php"><button id="lvbutton" style="float:right; margin- right:10px;">Sign-Out</button></a>

        </div>

            <div id="box">
                    <div id="name_box"><span>Welcome <?php echo($_SESSION['username']); ?></span></div>
                    <div id="box1">
                            <div> <p id="link">Your Information </p>

                                <ul style="margin-top:20px; padding-left:0;">
                                        <li><span>First Name : <?php echo $first_name; ?></span></li><br>
                                        <li><span>Last Name : <?php echo $last_name; ?></span></li><br>
                                        <li><span>Email : <?php echo $email; ?></span></li><br>
                                        <li><span>Age : <?php echo $m_no; ?></span></li><br>
                                        <li><span>Current Vehical : <?php echo $v_name;  ?></span></li><br>
                                        <li><span>Fuel Type: <?php echo $fuel_type; ?> </span></li><br>
                                        <li><span>Seating Capacity : <?php echo     $capacity; ?></span></li><br>
                                </ul>
                            </div>
                    </div>
                    <div id="box2">     <p id="link">Saved Routes </p>
                                <ul style="margin-top:20px;">
                                       <span> <?php foreach($row2 as $data) 
                                                    {echo "route Name : $data <br>"  ; }; 
                                                ?>
                                    </span>
                                </ul>
                    </div>   
            </div> 
         </div>
        </body> 
</html>

1 个答案:

答案 0 :(得分:0)

您对成员表的查询只会返回一行,因此您不需要在循环中使用$row = mysqli_fetch_array($result)(但不应该破坏您的脚本)。您对位置表的查询可能会返回多行,因此您需要循环访问该行。类似的东西:

$result2 = mysqli_query($con,"SELECT s_a_name FROM locations where  username='$fetch_row'");
$savedRoutes = "";

while($row2 = mysqli_fetch_array($result2))
{
  $savedRoutes .= "route Name : " . $row2['s_a_name'] . "<br/>";
}

将所有数据放在一个字符串($ savedRoutes)中,这些字符串可以在&#34; Saved Routes&#34;你的HTML代码部分。

或者,您可以将循环放在&#34; Saved Routes&#34;当你循环时,只是回显出每一行。我个人认为将循环放在顶部并将数据连接成一个字符串更清晰,更容易阅读。