title: Uncaught Error: Call to a member function query() on null
body: <p>I got stuck for hours in the code below. I don't know how I can fix this error.</p>
  <blockquote>
  <p>Notice: Undefined variable: mysqli in D:\xampp\htdocs\recon\register.php on line 19</p>
  <p>Fatal error: Uncaught Error: Call to a member function query() on null in D:\xampp\htdocs\recon\register.php:19 Stack trace: #0 {main} thrown in D:\xampp\htdocs\recon\register.php on line 19</p>
  </blockquote>
  <pre><code>&lt;?php
  
  $conn = new mysqli('localhost', 'root', '', 'user');
  
  if (mysqli_connect_errno()) {
      printf(&quot;Connect failed: %s\n&quot;, mysqli_connect_error());
      exit();
  }
  
  $uname = $_POST['uname'];
  $psw = $_POST['psw'];
  $options = [
  'cost' =&gt; 12,];
  $hashedpassword= password_hash($psw, PASSWORD_BCRYPT, $options);
  
  $result = $mysqli-&gt;query(&quot;SELECT username FROM registration WHERE username = '$uname'&quot;); 
  $row_count = $result-&gt;num_rows; 
  if($row_count == 1) { 
      echo 'User already exists, try another one.'; } 
  else {
  
      $query = &quot;INSERT INTO user (username, password) VALUES(?, ?)&quot;;
      $statement = $mysqli-&gt;prepare($query);
  
      $statement-&gt;bind_param('ss', $uname, $hashedpassword);
  
      if($statement-&gt;execute())
      {
           print 'Success! Last inserted record : ' .$statement-&gt;insert_id .'&lt;br /&gt;'; 
      }
      else
      {
           die('Error : ('. $mysqli-&gt;errno .') '. $mysqli-&gt;error);
      }
      $statement-&gt;close();
  
  }
  
  ?&gt;
  </code></pre>