Kaos
Kaos

Reputation: 173

Displaying data from mysql database in webpage using session

I simply need to display data from a mysql database. All the data is already stored in the database when the user submits the form. So in the landing page after the user is logged in, I need to display the users full name as well as a few other columns from the database table. In essence, I want the page to say Welcome fullname, then display some other colums from the database in a table. How should I code this using session? NOTE: I have tried to use sessions to display the user's full name and current balance after logging in. My code below:

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

Upvotes: 1

Views: 6119

Answers (4)

norbert
norbert

Reputation: 1

use this code, you put the session in curly braces.

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

Upvotes: 0

deviloper
deviloper

Reputation: 7240

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

Upvotes: 1

Devang Rathod
Devang Rathod

Reputation: 6736

When you logged in you need to store fullname in session like

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

After login , you can get this fullname on homepage.

$session_name = $_SESSION['fullname'];

Upvotes: 2

Tapas Pal
Tapas Pal

Reputation: 7207

During login just store the user id into session, so you could easily got all the information from database by this user id.

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

Upvotes: 0

Related Questions