title: OOP - Mysqi Connection: Uncaught Error: Call to undefined method DBConnection::prepare()
body: <p>I'm a newbie at PHP OOP and I have a problem with my code and i keep getting an error:</p>
  
  <blockquote>
    <p>Fatal error: Uncaught Error: Call to undefined method DBConnection::prepare() in E:\xampp\htdocs\includes\user_functions.php:35 Stack trace: #0 E:\xampp\htdocs\includes\process_login.php(12): User::login('admin', 'aa17a9b71574929...', Object(DBConnection)) #1 {main} thrown in E:\xampp\htdocs\includes\user_functions.php on line 35</p>
  </blockquote>
  
  <p>The error is in the prepare statement and i can't seem to figure out what i'm doing wrong. I can't seem to get my head around. I Assume it has something to do with the construct object</p>
  
  <p>I have already created a class for my connection, which i call upon in my login script. Once i am in the script, i call the login function. It seems to accept the connection, but the issue arises, when i call the prepare() function.</p>
  
  <p>Here is my login script:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
    include_once ('db_connect.php'); //Here i define my connection
    include_once ('user_functions.php'); //In this file my functions
  
    $mysqli = new DBConnection(); //i create a new object of the connection
  
    User::sec_session_start(); // This is the script in my user_functions.php
    if (isset($_POST['email'], $_POST['p'])) {
        $email = $_POST['email'];
        $password = $_POST['p']; 
  
        if (User::login($email, $password, $mysqli) == true) {
            header('Location: ../index.php');
        } else {
            header('Location: ../error.php?error=1');
        }
    } else {
        echo 'Invalid Request';
    }
  
  ?&gt;
  </code></pre>
  
  <p>Here is my db_connect.php</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
    include_once ('psl-config.php');   // Da functions.php nicht included ist
  
    class DBConnection {
  
      protected $mysqli;
      protected  $db_host = HOST;
      protected  $db_username = USER;
      protected  $db_password = PASSWORD;
      protected  $db_name = DATABASE;
  
      public function __construct() {
          $this-&gt;mysqli= new mysqli($this-&gt;db_host, $this-&gt;db_username, $this-&gt;db_password, $this-&gt;db_name)
          or die($this-&gt;mysqli-&gt;error);
  
           return $this-&gt;mysqli;
      }
  
      //$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE); //This method worked previously, when not done with OOP
      public function real_escape_string($str) {
        return $this-&gt;mysqli-&gt;real_escape_string();
      }
  
       function __destruct() {
         $this-&gt;mysqli-&gt;close();
       }
    }
  ?&gt;
  </code></pre>
  
  <p>Here is my user_functions.php</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  require_once ('db_connect.php');
    class User {
  
      public function __construct($mysqli) {
        return $this-&gt;mysqli=$mysqli;
      }
  
      function login($email, $password, $mysqli) {
        if ($stmt = $mysqli-&gt;prepare("SELECT user_id, username, password, salt //This is where the error occurs
            FROM users
            WHERE email = ? OR username = ?
            LIMIT 1")) {
            $stmt-&gt;bind_param('ss', $email, $email);
            $stmt-&gt;execute();
            $stmt-&gt;store_result();
            $stmt-&gt;bind_result($user_id, $username, $db_password);
            $stmt-&gt;fetch();
  
            if ($db_password == $password) {
              $user_browser = $_SERVER['HTTP_USER_AGENT'];
              $user_id = preg_replace("/[^0-9]+/", "", $user_id);
              $_SESSION['user_id'] = $user_id;
              $username = preg_replace("/[^a-zA-Z0-9_\-]+/",
                                                          "",
                                                          $username);
              $_SESSION['username'] = $username;
              $_SESSION['login_string'] = hash('sha512',
                        $password . $user_browser);
              return true;
            } else {
              return false;
            }
          }
        }
      }
  ?&gt;
  </code></pre>