title: PHP oop echo session username?
body: <p>I'm pretty clueless on how to retrieve or echo a username from the database, </p>
  
  <p>i have referenced <a href="https://stackoverflow.com/questions/4592081/php-oop-passing-session-key-between-classes">this</a></p>
  
  <p>and more similar questions like this, however i still don't understand how to echo out the current user username.</p>
  
  <p>everything works fine, i can login and logout</p>
  
  <p>just cant get find a way to echo current username</p>
  
  <p>here is the code </p>
  
  <p>User.php</p>
  
  <pre><code>public function get_user_sess()
  {
      try{
  
          $query = $this-&gt;db-&gt;prepare("SELECT * FROM users WHERE user_name=':username'");
          $query-&gt;execute(array(':username'=&gt;$username));
          $userRow = $query-&gt;fetch(PDO::FETCH_ASSOC);
  
          return $_SESSION['user_session'] = $userRow['username'];
  
      }
  
      catch(PDOExeception $e)
      {
          echo $e-&gt;getMessage();
      }
  }
  
  public function login($username, $password)
  {
      try{
          $stmt = $this-&gt;db-&gt;prepare("SELECT * FROM users WHERE user_name=:username OR user_pass=:password LIMIT 1");
          $stmt-&gt;execute(array(':username'=&gt;$username, ':password'=&gt;$password ));
          $userRow = $stmt-&gt;fetch(PDO::FETCH_ASSOC);
          if ($stmt-&gt;rowCount() &gt; 0)
          {
              if(password_verify($password,$userRow['user_pass']))
              {
                  $_SESSION['login'] = TRUE;
                  $_SESSION['user_session'] = $userRow['user_id'];
                  return true;
  
              }
  
              else{
                  return false;
              }
          }
      }
  
  
      catch(PDOExeception $e)
      {
          echo $e-&gt;getMessage();
      }
  }
  </code></pre>
  
  <p>Profile.php</p>
  
  <pre><code>&lt;?php
  session_start();
  error_reporting(-1);
  require_once 'User.php';
  
  $user = new User();
  if(isset($_GET['q'])){
     $user-&gt;logout();
     $user-&gt;redirect('login');
  }
  
  
  require_once 'layouts/header.php';
  ?&gt;
  &lt;h1&gt;Welcome 
  &lt;?php 
  
  $user = new User();
  echo $user-&gt;get_user_sess();
  ?&gt;
  &lt;/h1&gt;
  
  &lt;ul&gt;
      &lt;li&gt;&lt;a href="profile.php?q=logout"&gt;LOGOUT&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
  
  &lt;?php require_once ('layouts/footer.php');?&gt;
  </code></pre>
  
  <p>Login.php</p>
  
  <pre><code>&lt;?php
  session_start();
  error_reporting(-1);
  require 'Db.php';
  require 'User.php';
  
  
  
  if(isset($_POST['btn_login'])){
  
  
      $username = $_POST['txt_username'];
      $password = $_POST['txt_pass'];
  
  
      $guest = new User();
      if($username == "")
      {
          echo "Enter username please";
  
      }
  
      if($password == "")
      {
          echo "Enter password please";
      }
  
  
  
      if($guest-&gt;login($username, $password)){
          $guest-&gt;redirect('profile');      
      }
  
  
      else{
          echo "Invalid Entry";
      }
  
  
  
  
  
  }
  
  require_once 'layouts/header.php';
  
  ?&gt;
  
  &lt;div class="container"&gt;
      &lt;div class="row"&gt;
          &lt;h1&gt;Sign In&lt;/h1&gt;
  
  
          &lt;form action="" method="POST"&gt;
              &lt;div class="form-group"&gt;
                  &lt;label&gt;Username&lt;/label&gt;
                  &lt;input type="text" name="txt_username" class="form-control" required/&gt;
              &lt;/div&gt;
  
  
              &lt;div class="form-group"&gt;
                  &lt;label&gt;Password&lt;/label&gt;
                  &lt;input type="password" name="txt_pass" class="form-control" required/&gt;
              &lt;/div&gt;
  
  
           &lt;button type="submit" name="btn_login" class="btn btn-primary"&gt;Log In&lt;/button&gt;
  
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>