title: use session username to get data from mysql
body: <p>I am making a profile page for a user on my site, where the person can change his/her information. I should be possible to change, name, lastname, username, code etc. But I have started with the username.</p>
  
  <p>When a person log in, I store the username in session:</p>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>&lt;?php
      if( !isset( $_SESSION['username'])) include('resources/auth/login.php');
              else exit( header('Location: home.php') );
  
                  if( !isset( $_SESSION ) ) session_start();
  
      // Logout Session
      if( isset( $_GET['todo'] ) &amp;&amp; $_GET['todo'] == 'logout'){
  
          session_unset();
          session_destroy();
          //echo 'You have been logged out!';
      }
  ?&gt;
  </code></pre>
  
  <p>I have made a test on</p>
  
  <p><strong>profile.php:</strong></p>
  
  <pre><code>&lt;?php
     if( !isset( $_SESSION ) ) session_start();
  ?&gt;
     &lt;?php //echo $_SESSION['username'];?&gt; 
  </code></pre>
  
  <p>and this calls my username</p>
  
  <p>But If I want to call the firstname from mysql database, that belongs to that username, should I use the <code>session[username]</code> for that? I startet to make a SQL query for it instead:</p>
  
  <pre><code>&lt;?php
      $stmt = $mysqli-&gt;prepare("SELECT firstname FROM login");
  
      $stmt-&gt;execute();
  
      $fname = null;
  
        $stmt-&gt;bind_result($fname);
          while($stmt-&gt;fetch()) {
            echo $fname;
          }
  
      $stmt-&gt;close();
      $mysqli-&gt;close();
    ?&gt;
  </code></pre>
  
  <p>This works, but calls all the firstnames in the database. So I tried to set the u_id, which is the PK in db = to stored session variable:</p>
  
  <pre><code>$stmt = $mysqli-&gt;prepare("SELECT firstname FROM login WHERE u_id=$username");
  </code></pre>
  
  <p>and</p>
  
  <pre><code>$stmt = $mysqli-&gt;prepare("SELECT firstname FROM loginWHERE u_id = '.$_SESSION[username]'";
  </code></pre>
  
  <p>But here I get an error: 
  <em>Undefined variable: username in profile.php</em></p>
  
  <p>Can anybody see why that is?</p>