Home > Back-end >  How can I get data from MySQL for each user
How can I get data from MySQL for each user

Time:12-13

I need help. I crated a MySQL database and I connected database with login and registration form. All it´s working. I tried to create page that show all data about user from database. I created tried this code:

<p>Username: <?php echo $_SESSION['username']; ?></p>
<p>Email: <?php echo $_SESSION['email']; ?></p>
<p>Create profile date and time: <?php echo $_SESSION['create_datetime']; ?></p>

But that showed me only username. I created this from this page. Can you help me with this? Very much thanks for response!

CodePudding user response:

Inside login.php page you need to assign email and create_datetime to $_SESSION. It should be like

if ($rows == 1) {
        while($row = $result->fetch_assoc()) {
            $_SESSION['username'] = $row["username"];
            $_SESSION['email'] = $row["email"];
            $_SESSION['create_datetime'] = $row["create_datetime"];
        }
        // Redirect to user dashboard page
        header("Location: dashboard.php");
    }

CodePudding user response:

I am just adding values to a global $_SESSION variable from DB.

Change Registration.php File Like this

<?php
    require('db.php');
    session_start();
    if (isset($_POST['username'])) {
        $username = stripslashes($_REQUEST['username']);    // removes backslashes
        $username = mysqli_real_escape_string($con, $username);
        $password = stripslashes($_REQUEST['password']);
        $password = mysqli_real_escape_string($con, $password);
        $query    = "SELECT * FROM `users` WHERE username='$username'
                     AND password='" . md5($password) . "'";
        $result = mysqli_query($con, $query) or die(mysql_error());
        while($row = mysqli_fetch_assoc($result)){
//syntax: $_SESSION['var_name'] = $row['db's_column_name'];
            $_SESSION['email'] = $row['email'];             
            $_SESSION['create_datetime'] = $row['create_datetime'];
        }
    }else{
//Other Stuff
    }
?>
  • Related