Search code examples
phpmysql-real-escape-string

Call to a member function real_escape_string() on a non-object


The error is: Undefined variable: mysqli in /var/www/index.php on line 24 Fatal error: Call to a member function real_escape_string() on a non-object in /var/www/index.php on line 24

connect.php

<?php

/**
* Mysql Connect
*/

class MysqlConnect
{
    private $db_host;
    private $db_usermame;
    private $db_password;
    private $db_database;

    public function __construct($db_host,$db_usermame,$db_password,$db_database)
    {   
        $this->db_host = $db_host;
        $this->db_usermame = $db_usermame;
        $this->db_password = $db_password;
        $this->db_database = $db_database;

        $mysqli = mysqli_connect("$this->db_host", "$this->db_usermame", "$this->db_password", "$this->db_database") or die("Can't connect");
        $mysqli->select_db("$this->db_database") or die("Can't select database");
    }
}

$mysqli = new MysqlConnect('localhost','root','nistor','game');

?>

index.php

<?php

require_once ('connect.php');

?>

Register

<?php 

/**
* Register
*/


class RegisterUser
{   

    public $username;
    public $password;
    public $email;
    function verify(){
        if (isset($_POST['register'])) {
        return $username = $mysqli->real_escape_string($username);
        var_dump($mysqli); exit;
    }else{
        echo "no";
    }
}

}

$obj = new RegisterUser();
$obj->verify();

?>
<form action="index.php" method="POST">
Username: <input type="text" name="username"><br />
Password: <input type="password" name="password"><br />
E-Mail: <input type="text" name="email"><br />
<input type="submit" name="register" value="Register">
</form>

Solution

  • Your verify function does not have an object called $mysqli, it only exist in your MysqlConnect class's constructor. You can readjust your MysqlConnect class as follows.

    class MysqlConnect
    {
        private $db_host;
        private $db_usermame;
        private $db_password;
        private $db_database;
        private $mysqli;
    
        public function __construct($db_host,$db_usermame,$db_password,$db_database)
        {   
            $this->db_host = $db_host;
            $this->db_usermame = $db_usermame;
            $this->db_password = $db_password;
            $this->db_database = $db_database;
    
            $this->mysqli = mysqli_connect("$this->db_host", "$this->db_usermame", "$this->db_password", "$this->db_database") or die("Can't connect");
            $this->mysqli->select_db("$this->db_database") or die("Can't select database");
        }
    
        public function getMysqli()
        {
            return $this->mysqli;
        }
    }
    

    Once you have done this, then you can do something similar to the following code in your driver class which is registeruser.

    class RegisterUser
    {   
    
        public $username;
        public $password;
        public $email;
        function verify(){
            $mysqlcon = new MysqlConnect('localhost','root','nistor','game');
            $mysqli = $mysqlcon->getMysqli();
            if (isset($_POST['register'])) {
            return $username = $mysqli->real_escape_string($username);
            var_dump($mysqli); exit;
        }else{
            echo "no";
        }
    }
    
    }