title: Why prepared statement doesn't work?
body: <p>I am using this code to check if the user is logged:</p>
  
  <pre><code>    function login_check_admin($mysqli) {
     // Check if all session variables are set
     if(isset($_SESSION['user_id'], $_SESSION['username'], $_SESSION['login_string'])) {
  
       $user_id = $_SESSION['user_id'];
       $login_string = $_SESSION['login_string'];
       $username = $_SESSION['username'];
  
       $user_browser = $_SERVER['HTTP_USER_AGENT']; // Get the user-agent string of the user.
  
       if ($stmt = $mysqli-&gt;prepare("SELECT admin_pas FROM admins WHERE admin_id = ? LIMIT 1")) { 
          $stmt-&gt;bind_param('i', $user_id); // Bind "$user_id" to parameter.
          $stmt-&gt;execute(); // Execute the prepared query.
          $stmt-&gt;store_result();
  
          if($stmt-&gt;num_rows == 1) { // If the user exists
             $stmt-&gt;bind_result($password); // get variables from result.
             $stmt-&gt;fetch();
  
             $login_check = hash('sha512', $password.$user_browser);
  
             if($login_check == $login_string) {
                // Logged In!!!!
                return true;
             } else {
                // Not logged in
                return false;
             }
          } else {
              // Not logged in
              return false;
          }
       } else {
          // Not logged in
          return false;
       }
     } else {
       // Not logged in
       return false;
     }
  }
  </code></pre>
  
  <p>Problem is it only works with the last member thats is added to the <code>admins</code> table. As soon as I add another member to the <code>admins</code> table, it returns false when I am logged in with all other members and returns true only when I am logged in with that most recently added member. I changed the code to this and now it works fine. I don't know why prepared statement not work.</p>
  
  <pre><code>function login_check_admin($mysqli) {
     // Check if all session variables are set
     if(isset($_SESSION['user_id'], $_SESSION['username'], $_SESSION['login_string'])) {
       $user_id = $_SESSION['user_id'];
       $login_string = $_SESSION['login_string'];
       $username = $_SESSION['username'];
  
       $user_browser = $_SERVER['HTTP_USER_AGENT']; // Get the user-agent string of the user.
       if ($result = $mysqli-&gt;query("SELECT admin_pas FROM admins WHERE admin_id = ? LIMIT 1")) { 
          if($obj = $result-&gt;fetch_object()) { // If the user exists
             $password = $obj-&gt;admin_pas; 
             unset($obj);
             $result-&gt;close();
             $login_check = hash('sha512', $password.$user_browser);
             if($login_check == $login_string) {
                // Logged In!!!!
  
                return true;
             } else {
                // Not logged in
                return false;
             }
          } else {
              // Not logged in
              return false;
          }
       } else {
          // Not logged in
          return false;
       }
     } else {
       // Not logged in
       return false;
     }
  } 
  </code></pre>