title: Fetch row and display using sessions
body: <p>been working on this project where I want people to login and when successful see info that we get from the database. I am not sure how to get the user email and other data stored to session</p>
  
  <p>My problem is that I am struggling to store the data in sessions. I can sign in and echo the username that I created a session for, but the other data is not working. </p>
  
  <p>I have gone through stacks of stuff on here, but obviously I am a little lost.</p>
  
  <p>Here is the code for my checklogin.php</p>
  
  <pre><code>&lt;?php
  
  session_start();
  ob_start();
  include_once 'config.php';
  
  // Connect to server and select databse.
  try
  {
      $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
      $db = new PDO('mysql:host='.$host.';dbname='.$db_name.';charset=utf8', $username, $password);
  }
  catch(Exception $e)
  {
      die('Error : ' . $e-&gt;getMessage());
  }   
  
  // Define $myusername and $mypassword 
  $myusername = $_POST['myusername']; 
  $mypassword = $_POST['mypassword'];
  $myemail = 
  
  // To protect MySQL injection
  $myusername = stripslashes($myusername);
  $mypassword = stripslashes($mypassword);
  
  $stmt = $db-&gt;query("SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'");
  
  
  // rowCount() is counting table row
  $count = $stmt-&gt;rowCount();
  
  // If result matched $myusername and $mypassword, table row must be 1 row
  if($count == 1){
  
      // Register $myusername, $mypassword and print "true"
      echo "true";
      $_SESSION['username'] = $myusername;
      $_SESSION['email'] = $myemail;
  
  }
  else {
      //return the error message
      echo "&lt;div class=\"alert alert-danger alert-dismissable\"&gt;&lt;button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\"&gt;&amp;times;&lt;/button&gt;Wrong Username or Password&lt;/div&gt;";
  }
  
  ob_end_flush();
  </code></pre>
  
  <p>?></p>
  
  <p>and then my index.php looks like this </p>
  
  <pre><code>&lt;?php
    session_start();
    if(!isset($_SESSION['username'])){
      header("location:main_login.php");
    }
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
  &lt;meta charset="utf-8"&gt;
  &lt;title&gt;Login&lt;/title&gt;
  &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
  &lt;!-- Bootstrap --&gt;
  &lt;link href="css/bootstrap.css" rel="stylesheet" media="screen"&gt;
  &lt;link href="css/main.css" rel="stylesheet" media="screen"&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div class="container"&gt;
    &lt;div class="form-signin"&gt;
      &lt;div class="alert alert-success"&gt;You have been &lt;strong&gt;successfully&lt;/strong&gt; logged in as &lt;?php echo $_SESSION['username']; ?&gt;. Your email is &lt;?php echo $row['email']; ?&gt;&lt;/div&gt;
  
  
  
  
  
      &lt;a href="logout.php" class="btn btn-default btn-lg btn-block"&gt;Logout&lt;/a&gt; &lt;/div&gt;
  
  &lt;/div&gt;
  &lt;!-- /container --&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Note that on index.php the username works fine when I echo it, but not the email. </p>
  
  <p>Any help would be appreciated </p>