title: Fatal error: Call to a member function prepare() on null in PDO
body: <p>I'm trying to make a login in PHP and PDO but I get</p>
  
  <blockquote>
    <p>Fatal error: Call to a member function prepare() on null in
    DBOperations.php on line 23</p>
  </blockquote>
  
  <p>the whole time. I've been looking for a solution on the internet, but I only found some errors I didn't have, like <a href="https://stackoverflow.com/questions/30627027/fatal-error-call-to-a-member-function-prepare-on-null">Fatal error Call to a member function prepare() on null</a>. Can someone please help me?</p>
  
  <p>Constants.php:</p>
  
  <pre><code>&lt;?php
  
  define('DB_NAME', 'roedel');
  define('DB_USER', 'root');
  define('DB_HOST', 'localhost');
  define('DB_PASS', '');
  
  ?&gt;
  </code></pre>
  
  <p>DBConnect.php:</p>
  
  <pre><code>&lt;?php
  
  class DBConnect {
  
      private $con;
  
      function __construct() {
  
      }
  
      function connect() {
  
          require_once dirname(__FILE__).'/Constants.php';
  
          try{
  
              $this-&gt;con = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS);
  
          } catch (Exception $ex) {
  
              echo "No connection. Try again!" . $ex;
  
          }
  
  
  
      }    
  }
  
  ?&gt;
  </code></pre>
  
  <p>DBOperations.php:</p>
  
  <pre><code>&lt;?php
  
  class DBOperations {
  
      private $con;
  
      function __construct() {
  
          require_once dirname(__FILE__).'/DBConnect.php';
  
          $db = new DBConnect();
  
          $this-&gt;con = $db-&gt;connect();
  
      }
  
      function createUser($name, $pass, $email){
  
          $password = md5($pass);
  
          $rank = "lid";
  
          $stmt = $this-&gt;con-&gt;prepare("INSERT INTO 'users' ('id', 'name', 'password', 'email', 'rank') VALUES (NULL, ?, ?, ?, ?)");
          $stmt-&gt;bindParam(1, $name);
          $stmt-&gt;bindParam(2, $password);
          $stmt-&gt;bindParam(3, $email);
          $stmt-&gt;bindParam(4, $rank);
  
          if ($stmt-&gt;execute()){
  
              return true;
  
          }else{
  
              return false;
  
          }
  
  
      }
  
  }
  
  ?&gt;
  </code></pre>