使用session在网页中显示mysql数据库中的数据

时间:2013-03-05 08:15:57

标签: php mysql session

我只需要显示mysql数据库中的数据。当用户提交表单时,所有数据都已存储在数据库中。因此,在用户登录后的登录页面中,我需要显示用户全名以及数据库表中的其他几列。本质上,我希望页面说欢迎 fullname ,然后在表格中显示数据库中的其他一些列。我应该如何使用会话编码? 注意:我已尝试使用会话在登录后显示用户的全名和当前余额。 我的代码如下:

<?php
    // Connect to database display welcome Full name, then date, name, credit, debit, balance
    session_start();
    $fullname="";

    $currentbalance="";
    if (!isset($_SESSION)){
    session_start();
    }
    echo $_SESSION['fullname'];


    ?>
    Welcome <?php echo $_SESSION['fullname']; ?>.
    <table border ="1">
    <th>DATE </th>
    <th>'.$fullname.' </th>
    <th>CREDIT </th>
    <th>DEBIT</th>
    <th><?php echo $_SESSION['currentbalance']; ?</th>
    </table>

4 个答案:

答案 0 :(得分:2)

登录后,您需要将fullname存储在session

$_SESSION['fullname'] = $_REQUEST['fullname'];

login之后,您可以在主页上获得此fullname

$session_name = $_SESSION['fullname'];

答案 1 :(得分:1)

<?php
    // Connect to database display welcome Full name, then date, name, credit, debit, balance
    if (!isset($_SESSION)){
    session_start();
    }
    $fullname="";
    $currentbalance="";

    $_SESSION['fullname']=$fullname;
$_SESSION['currentbalance ']=$currentbalance ; // Where $fullname and $currentbalance must be already defined by the query


    ?>
    Welcome <?php echo $_SESSION['fullname']; ?>.
    <table border ="1">
    <th>DATE </th>
    <th>'.$fullname.' </th>
    <th>CREDIT </th>
    <th>DEBIT</th>
    <th><?php echo $_SESSION['currentbalance']; ?</th>
    </table>

答案 2 :(得分:0)

在登录期间,只需将用户ID存储到会话中,这样您就可以通过此用户ID轻松获取数据库中的所有信息。

"select * from `users` where id='".$_SESSION['userid']."'"

答案 3 :(得分:0)

使用此代码,您将会话放在花括号中。

"select * from `users` where id={$_SESSION['userid']}"
相关问题