回声没有传递var传递而

时间:2016-01-29 02:08:58

标签: php html mysql html5 session

我试图获得echo $ userRow [' student_firstname'];传入html所以我可以用它作为一种自动方式来改变显示用户的名字&使用寄存器注销&登录。第一个echo $ userRow [' student_firstname'];确实有效,但第二个没有。这个想法可以让第二个工作,所以我可以删除第一个。如果没有会话,则显示登录&寄存器。

    <?php
session_start();
include_once 'dbconnect_new.php';
?>

    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimal-ui">
        <link href="favicon.png" type="image/x-icon" rel="shortcut icon">
        <link href="assets/css/master.css" rel="stylesheet">
        <script src="assets/plugins/jquery/jquery-1.11.3.min.js"></script>
    </head>

    <?php if(isset($_SESSION['user'])) { 
    // error_reporting(E_ALL ^ E_DEPRECATED);
    $res=mysql_query("SELECT * FROM studentdata WHERE student_id=".$_SESSION['user']); 
    if ($res === FALSE){
        die(mysql_error());
    } 
    while($userRow=mysql_fetch_array($res))
    {
        echo $userRow['student_firstname'];
    }
    ?>
        <li class="dropdown">
            <a href="">
                <?php echo $userRow['student_firstname'];?><span class="nav-subtitle">Account</span></a</li>
                    <li><a href="includes/logout.php" title="">Logout<span class="nav-subtitle">Goodbye</span></a></li>
                    <?php } else { ?>
                        <li><a href="includes/register_new.php" title="">Register<span class="nav-subtitle">for Students</span></a></li>
                        <li><a href="includes/login_new.php" title="">Login<span class="nav-subtitle">for Students</span></a></li>
                        <?php  }  ?>

    </html>

1 个答案:

答案 0 :(得分:0)

检查此链接http://php.net/manual/en/function.mysql-fetch-array.php, 提示:不推荐使用mysqli / pdo作为mysql。

<?php
session_start();
include_once 'dbconnect_new.php';
?>

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimal-ui">
        <link href="favicon.png" type="image/x-icon" rel="shortcut icon">
        <link href="assets/css/master.css" rel="stylesheet">
        <script src="assets/plugins/jquery/jquery-1.11.3.min.js"></script>
    </head>

    <?php
    if (isset($_SESSION['user'])) {
        // error_reporting(E_ALL ^ E_DEPRECATED);
        $res = mysql_query("SELECT * FROM studentdata WHERE student_id=" . $_SESSION['user']);
        if ($res === FALSE) {//IF QUERY RETURNS NULL
            die(mysql_error());
        } else {
            $userRow = mysql_fetch_array($res, MYSQL_ASSOC); //DATA IN ARRAY TYPE
            //IF BELOW DOES NOT WORK USE print_r(); to check the structure of array !
            ?>
            <li class="dropdown">
                <a href=""><?php echo $userRow['student_firstname']; //ACCESS DATA THROUGH ITS INDEX   ?><span class="nav-subtitle">Account</span></a>
            </li>
            <li>
                <a href="includes/logout.php" title="">Logout<span class="nav-subtitle">Goodbye</span></a>
            </li>
            <?php
        }
    } else {
        ?>
        <li><a href = "includes/register_new.php" title = "">Register<span class = "nav-subtitle">for Students</span></a></li>
        <li><a href = "includes/login_new.php" title = "">Login<span class = "nav-subtitle">for Students</span></a></li>
        <?php } ?>
</html>

我希望这有效!