title: PHP Displaying user info when logged in
body: <p>Alright, I have tried and searched everywhere to fix this but no luck.</p>
  
  <p>All I am trying to do is display a users username and email (Who are logged in) and then print their details to thier account page. 
  <strong>The problem is that all of the users in the database are being logged, I only want the users who is logged in to be displayed.</strong> </p>
  
  <p><strong>Db.php</strong></p>
  
  <pre><code>&lt;?php
  $myConnection= mysqli_connect("localhost","root","") or die ("could not connect to mysql");
  
  mysqli_select_db($myConnection, "register") or die ("no database");
  &gt;
  </code></pre>
  
  <p><strong>Auth.php</strong></p>
  
  <pre><code>&lt;?php
  session_start();
  if(!isset($_SESSION["username"])){
  header("Location: login.php");
  exit(); }
  ?&gt;
  </code></pre>
  
  <p><strong>Login.php</strong></p>
  
  <pre><code>&lt;?php
    require('db.php');
      session_start();
      // If form submitted, insert values into the database.
      if (isset($_POST['username'])){
          $username = $_POST['username'];
          $password = $_POST['password'];
          $username = stripslashes($username);
          $username = mysqli_real_escape_string($myConnection, $username);
          $password = stripslashes($password);
          $password = mysqli_real_escape_string($myConnection, $password);
      //Checking is user existing in the database or not
          $query = "SELECT * FROM `users` WHERE username='$username' and password='".md5($password)."'";
          $result = mysqli_query($myConnection, $query) or die(mysqli_error());
          $rows = mysqli_num_rows($result);
          if($rows==1){
              $_SESSION['username'] = $username;
        $_SESSION['user_id'] = $row['user_id'];
              header("Location: index.php"); // Redirect user to index.php
              }else{
                  echo "&lt;div class='form'&gt;&lt;h3&gt;Username/password is incorrect.&lt;/h3&gt;&lt;br/&gt;Click here to &lt;a href='login.php'&gt;Login&lt;/a&gt;&lt;/div&gt;";
                  }
      }else{
  ?&gt;
  </code></pre>
  
  <p><strong>Register.php</strong> </p>
  
  <pre><code>&lt;?php
      require('db.php');
      // If form submitted, insert values into the database.
      if (isset($_POST['username'])){
          $username = $_POST['username'];
          $email = $_POST['email'];
          $password = $_POST['password'];
          $username = stripslashes($username);
          $username = mysqli_real_escape_string($myConnection, $username);
          $email = stripslashes($email);
          $email = mysqli_real_escape_string($myConnection, $email);
          $password = stripslashes($password);
          $password = mysqli_real_escape_string($myConnection, $password);
          $trn_date = date("Y-m-d H:i:s");
          $query = "INSERT into `users` (username, password, email, trn_date) VALUES ('$username', '".md5($password)."', '$email', '$trn_date')";
          $result = mysqli_query($myConnection, $query);
          if($result){
              echo "&lt;div class='form'&gt;&lt;h3&gt;You are registered successfully.&lt;/h3&gt;&lt;br/&gt;Click here to &lt;a href='login.php'&gt;Login&lt;/a&gt;&lt;/div&gt;";
          }
      }else{
  ?&gt;
  </code></pre>
  
  <p>Account.php //Where I want user data to be displayed on page</p>
  
  <pre><code>&lt;?php
      // SQL query
      $strSQL = "SELECT * FROM users";
  
      // Execute the query (the recordset $rs contains the result)
      $rs = mysqli_query($myConnection, $strSQL);
  
      // Loop the recordset $rs
      // Each row will be made into an array ($row) using mysqli_fetch_array
      while($row = mysqli_fetch_array($rs)) {
  
         // Write the value of the column FirstName (which is now in the array $row)
        echo $row['username'] . "&lt;br /&gt;";
        echo $row['email'] . "&lt;br /&gt;";
        }
  
      // Close the database connection
      mysqli_close($myConnection);
      ?&gt;
  </code></pre>