title: Fatal error: Call to a member function query() on null
body: <p>I'm not sure what's going wrong here. I was just following a tutorial online and these errors popped up.</p>
  
  <p>I'm getting the following errors</p>
  
  <p><strong>Error</strong></p>
  
  <pre><code>Notice: Undefined variable: db in C:\xampp\htdocs\wisconsindairyfarmers\admin\login.php on line 7
  
  Fatal error: Call to a member function query() on null in C:\xampp\htdocs\wisconsindairyfarmers\admin\login.php on line 7
  </code></pre>
  
  <p><strong>Code</strong></p>
  
  <pre><code>&lt;?php
  $db = new mysqli('127.0.0.1', 'root', '', 'wisconsindairyfarmers');
  ?&gt;
  
  &lt;?php
  require '../db/connect.php';
  require '../functions/general.php';
  
      function user_exists($username){
          //$username = sanitize($username);
          $result = $db-&gt;query("SELECT COUNT(UserId) FROM users WHERE UserName = '$username'");
          if($result-&gt;num_rows){
          return (mysqli_result($query, 0) == 1) ? true : false;
      }}
  
  if(empty($_POST) === false){
  
      $username = $_POST['username'];
      $password = $_POST['password'];
  
      if(empty($username) === true || empty($password) === true){ 
          echo 'You need to enter a username and password';
      }
      else if(user_exists($username) === false) {
          echo 'We can\'t find that username.';
      }
  }
  
  ?&gt;
  </code></pre>