Uncaught Error: Call to a member function prepare() on null. Can someone please help me with these? [closed]

April 15, 2021, at 9:10 PM
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.

Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 16 hours ago.

Improve this question

When I run the file user.php, this error is shown:

DB Successfully Connected. Fatal error: Uncaught Error: Call to a member function prepare() on null in E:\xampp\htdocs\StieUX-Inventory\public_html\includes\user.php:40 Stack trace: #0 E:\xampp\htdocs\StieUX-Inventory\public_html\includes\user.php(53): User->emailExists('user@gmail.com') #1 E:\xampp\htdocs\StieUX-Inventory\public_html\includes\user.php(74): User->createUserAccount('Ram user', 'user@gmail.com', '123456789', 'Admin', 'Baluwatar') #2 {main} thrown in E:\xampp\htdocs\StieUX-Inventory\public_html\includes\user.php on line 40

These are the codes used:


    define("HOST", "localhost");
    define("USER", "root");
    define("PASS", "");
    define("DB", "SiteUX_Inventory_DB");


class Database
    private $con;
    public function connect()
        include_once "constants.php";
        $this->con = new Mysqli(HOST, USER, PASS, DB);
        if ($this->con) {
            echo "DB Successfully Connected.";
            return $this->con;
        } else {
            return "DATABASE_CONENCTION_FAILED";



    class User 
        public $con;
        function __construct()
            $db = new Database();
            try {
                $this->db = $db->connect();
            } catch (\Throwable $th) {
                echo $th;
        // Check if User EmailExists
        private function emailExists ($email){
            $pre_stmt = $this->con->prepare("SELECT id FROM user WHERE email = ?");
            $pre_stmt->bind_param('s', $email);
            $pre_stmt->execute() or die($this->con->error);
            $result = $pre_stmt->get_result();
            if ($result->num_rows > 0) {
                return 1;
                return 0;
        public function createUserAccount($name,$email,$password,$user_type,$address){
            if ($this->emailExists($email)) {
            } else {
                $date = date("Y-m-d-hh-mm");
                $pass_hash = password_hash($password, PASSWORD_BCRYPT, ["cost"=>8]);
                $pre_stmt = $this->con->prepare("INSERT INTO 'user'('name', 'password', 'email', 'address', 'user_type', 'last_login', 'register_date') VALUES (?,?,?,?,?,?,?)");
                $pre_stmt->bind_param('sssssss', $name, $pass_hash, $email, $address, $user_type, $date, $date);
                $result = $pre_stmt->execute() or die($this->con->error);
                if ($result) {
                    return $this->con->insert_id;
                    return $this->con->error;
    $user5 = new User();
    echo $user5->createUserAccount("Ram user","user@gmail.com","123456789","Admin","Baluwatar");

Can someone please help me out with this?