title: How to get specific row using prepared statement to store it in a session?
body: <p>How do you get specific row using the prepared statement to store it inside a session? I'm having a problem using it.</p>
  
  <p>here's my code.</p>
  
  <p><strong>PHP Code</strong></p>
  
  <pre><code>&lt;?php
  include "connection.php";
  session_start();
  
  $username = $_POST['username'];
  $password = $_POST['password'];
  
  if($username == $_POST['username'] &amp;&amp; $password == $_POST['password'])
  {
    // prepare an sql statement
    $stmt = $conn-&gt;prepare("select username, 
                                firstname, 
                                lastname, 
                                email 
                            from users 
                            where username = ? and password = ?");
    //bind the sql statement
    $stmt-&gt;bind_param("ss", $myusername, $mypassword);
  
    // set parameters, and to protect sql injection
    $myusername = stripcslashes($username);
    $password = stripcslashes($password);
  
    $myusername = mysqli_real_escape_string($conn,$username);
    $mypassword = mysqli_real_escape_string($conn,$password);
  
    $_SESSION['user'] = $myusername;
    $_SESSION['password'] = $mypassword;
  
    $result = $stmt-&gt;get_result();
  
    $output = array();
    while ($row = $result-&gt;fetch_assoc())
    {
        $_SESSION['firstname'] = $row['firstname'];
    }
  
    header('Location: /laurence/FirstDayTraining/Model/users.php');
  
  
  }
  else {
    echo "Not an admin";
  }
  $stmt-&gt;close();
  $conn-&gt;close();
  
  ?&gt;
  </code></pre>
  
  <p>I've tried using this:</p>
  
  <pre><code>  $result = $stmt-&gt;get_result();
  
    $output = array();
    while ($row = $result-&gt;fetch_assoc())
    {
        $_SESSION['firstname'] = $row['firstname'];
    }
  </code></pre>
  
  <p>But I get the result a fatal error</p>
  
  <pre><code>Fatal error: 
      Call to a member function fetch_assoc() on a non-object in loginbackend.php on line 28
  </code></pre>
  
  <p>What seems to be the problem? Please help</p>