title: Registration Form: function prepare() on null
body: <p>I am trying to create a registration form (using an SQL database) that utilizes my 
  "register.inc.php" file, When I attempt to create an account (as a test) I get </p>
  
  <p><strong>THIS error:</strong> 
  "Fatal error: Call to a member function prepare() on null in /usr/www/world/includes/register.inc.php on line 30"</p>
  
  <p>I've posted the code from my register.inc.php, any insight would be much appreciated.</p>
  
  <pre><code>    &lt;?php
  include_once 'loginhandler.php';
  include_once 'DBclass.php';
  
  $error_msg = "";
  
  if (isset($_POST['username'], $_POST['email'], $_POST['p'])) {
      // Sanitize and validate the data passed in
      $username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
      $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
      $email = filter_var($email, FILTER_VALIDATE_EMAIL);
      if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
          // Not a valid email
          $error_msg .= '&lt;p class="error"&gt;The email address you entered is not valid&lt;/p&gt;';
      }
  
      $password = filter_input(INPUT_POST, 'p', FILTER_SANITIZE_STRING);
      if (strlen($password) != 128) {
          // The hashed pwd should be 128 characters long.
          // If it's not, something really odd has happened
          $error_msg .= '&lt;p class="error"&gt;Invalid password configuration.&lt;/p&gt;';
      }
  
      // Username validity and password validity have been checked client side.
      // This should should be adequate as nobody gains any advantage from
      // breaking these rules.
      //
  
      $prep_stmt = "SELECT id FROM members WHERE email = ? LIMIT 1";
      $stmt = $mysqli-&gt;prepare($prep_stmt);
  
     // check existing email  
      if ($stmt) {
          $stmt-&gt;bind_param('s', $email);
          $stmt-&gt;execute();
          $stmt-&gt;store_result();
  
          if ($stmt-&gt;num_rows == 1) {
              // A user with this email address already exists
              $error_msg .= '&lt;p class="error"&gt;A user with this email address already exists.&lt;/p&gt;';
              $stmt-&gt;close();
          }
      } else {
          $error_msg .= '&lt;p class="error"&gt;Database error Line 39&lt;/p&gt;';
          $stmt-&gt;close();
      }
  
      // check existing username
      $prep_stmt = "SELECT id FROM members WHERE username = ? LIMIT 1";
      $stmt = $mysqli-&gt;prepare($prep_stmt);
  
      if ($stmt) {
          $stmt-&gt;bind_param('s', $username);
          $stmt-&gt;execute();
          $stmt-&gt;store_result();
  
          if ($stmt-&gt;num_rows == 1) {
              // A user with this username already exists
              $error_msg .= '&lt;p class="error"&gt;A user with this username already exists&lt;/p&gt;';
              $stmt-&gt;close();
          }
      } else {
          $error_msg .= '&lt;p class="error"&gt;Database error line 55&lt;/p&gt;';
          $stmt-&gt;close();
      }
  
      // TODO: 
      // We'll also have to account for the situation where the user doesn't have
      // rights to do registration, by checking what type of user is attempting to
      // perform the operation.
  
      if (empty($error_msg)) {
  
          // Create hashed password using the password_hash function.
          // This function salts it with a random salt and can be verified with
          // the password_verify function.
          $password = password_hash($password, PASSWORD_BCRYPT);
  
          // Insert the new user into the database 
          if ($insert_stmt = $mysqli-&gt;prepare("INSERT INTO members (username, email, password) VALUES (?, ?, ?)")) {
              $insert_stmt-&gt;bind_param('sss', $username, $email, $password);
              // Execute the prepared query.
              if (! $insert_stmt-&gt;execute()) {
                  header('Location: ../error.php?err=Registration failure: INSERT');
              }
          }
          header('Location: ./register_success.php');
      }
  }
  ?&gt;
  </code></pre>