title: PHP PDO Declare name as variable
body: <p>So In my project I'm trying to declare a variable so it will display their full name if logged in. I'm assuming it would be a query to fetch the data from the table but I'm unsure on how to have it make sure it get's that certain user's name and not the first name on the table</p>
  
  <p>Here's my registration code.</p>
  
  <pre><code>if( $user-&gt;is_logged_in() ){ header('Location: index.php'); } 
  </code></pre>
  
  <p>//if form has been submitted process it
  if(isset($_POST['submit'])){</p>
  
  <pre><code>//very basic validation
  if(strlen($_POST['username']) &lt; 3){
      $error[] = 'Username is too short.';
  } else {
      $stmt = $db-&gt;prepare('SELECT username FROM members WHERE username = :username');
      $stmt-&gt;execute(array(':username' =&gt; $_POST['username']));
      $row = $stmt-&gt;fetch(PDO::FETCH_ASSOC);
  
      if(!empty($row['username'])){
          $error[] = 'Username provided is already in use.';
      }
  
  }
  
  if(strlen($_POST['password']) &lt; 3){
      $error[] = 'Password is too short.';
  }
  
  if(strlen($_POST['passwordConfirm']) &lt; 3){
      $error[] = 'Confirm password is too short.';
  }
  
  if($_POST['password'] != $_POST['passwordConfirm']){
      $error[] = 'Passwords do not match.';
  }
  
  //email validation
  if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
      $error[] = 'Please enter a valid email address';
  } else {
      $stmt = $db-&gt;prepare('SELECT email FROM members WHERE email = :email');
      $stmt-&gt;execute(array(':email' =&gt; $_POST['email']));
      $row = $stmt-&gt;fetch(PDO::FETCH_ASSOC);
  
      if(!empty($row['email'])){
          $error[] = 'Email provided is already in use.';
      }
  
  }
      //email validation
  if(strlen($_POST['fullname']) &lt; 2){
      $error[] = 'Please enter a valid full name';
  } else {
      $stmt = $db-&gt;prepare('SELECT fullname FROM members WHERE fullname = :fullname');
      $stmt-&gt;execute(array(':fullname' =&gt; $_POST['fullname']));
      $row = $stmt-&gt;fetch(PDO::FETCH_ASSOC);
  
      if(!empty($row['email'])){
          $error[] = 'Email provided is already in use.';
      }
  
  }
  
  //if no errors have been created carry on
  if(!isset($error)){
  
      //hash the password
      $hashedpassword = $user-&gt;password_hash($_POST['password'], PASSWORD_BCRYPT);
  
      //create the activasion code
      $activasion = md5(uniqid(rand(),true));
  
      try {
  
          //insert into database with a prepared statement
          $stmt = $db-&gt;prepare('INSERT INTO members (username,password,email,fullname,active) VALUES (:username, :password, :email, :fullname, :active)');
          $stmt-&gt;execute(array(
              ':username' =&gt; $_POST['username'],
              ':password' =&gt; $hashedpassword,
              ':email' =&gt; $_POST['email'],
              ':fullname' =&gt; $_POST['fullname'],
              ':active' =&gt; $activasion
          ));
          $id = $db-&gt;lastInsertId('memberID');
  
          //send email
          $to = $_POST['email'];
          $subject = "Registration Confirmation";
          $body = "Thank you for registering at demo site.\n\n To activate your account, please click on this link:\n\n ".DIR."activate.php?x=$id&amp;y=$activasion\n\n Regards Site Admin \n\n";
          $additionalheaders = "From: &lt;".SITEEMAIL."&gt;\r\n";
          $additionalheaders .= "Reply-To: $".SITEEMAIL."";
          mail($to, $subject, $body, $additionalheaders);
  
          //redirect to index page
          header('Location: index.php?action=joined');
          exit;
  
      //else catch the exception and show the error.
      } catch(PDOException $e) {
          $error[] = $e-&gt;getMessage();
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Updated with the user class:</p>
  
  <pre><code>&lt;?php
  </code></pre>
  
  <p>include('password.php');
  class User extends Password{</p>
  
  <pre><code>private $_db;
  
  function __construct($db){
      parent::__construct();
  
      $this-&gt;_db = $db;
  }
  
  private function get_user_hash($username){  
  
      try {
          $stmt = $this-&gt;_db-&gt;prepare('SELECT password FROM members WHERE username = :username');
          $stmt-&gt;execute(array('username' =&gt; $username));
  
          $row = $stmt-&gt;fetch();
          return $row['password'];
  
      } catch(PDOException $e) {
          echo '&lt;p class="bg-danger"&gt;'.$e-&gt;getMessage().'&lt;/p&gt;';
      }
  }
  
  public function login($username,$password){
  
      $hashed = $this-&gt;get_user_hash($username);
  
      if($this-&gt;password_verify($password,$hashed) == 1){
  
          $_SESSION['loggedin'] = true;
          return true;
      }   
  }
  
  public function logout(){
      session_destroy();
  }
  
  public function is_logged_in(){
      if(isset($_SESSION['loggedin']) &amp;&amp; $_SESSION['loggedin'] == true){
          return true;
      }
  
  }
  </code></pre>
  
  <p>}</p>