title: PHP SQL login error
body: <p>I'm trying to create a login page using php and sql.  This is my php code that gets the username and password data from mysql and checks if they are right so the user gets logged in. I'm getting this error:</p>
  
  <blockquote>
    <p>Call to a member function fetch() on a non-object</p>
  </blockquote>
  
  <p>on the line:</p>
  
  <pre><code>$row = $result-&gt;fetch(PDO::FETCH_ASSOC);
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>&lt;?php session_start();
  
  $db = new PDO("mysql:host=localhost;dbname=database","user","pass");
    if(mysqli_connect_errno())
    {
      echo "fail!". mysqli_connect_errnor();
    }
  
  $first =$_POST['username'];
  $last=$_POST['password'];
  
  $sql="SELECT count(user_id) as records FROM table WHERE username='$first' AND password='$last'";
  $result = $db-&gt;query($sql);
  $row = $result-&gt;fetch(PDO::FETCH_ASSOC);
  if ($row['records'] == 1);
  {
      $_SESSION['loggedIN'] =true;
      header("Location:logged.php");
      die();
  
  }
  else {
  
      $_SESSION['loggedIN'] = null;
      header("Location:home.php");
      die();
  
  }
  
  
  ?&gt;
  </code></pre>