I am trying to use mysqli and it has been giving me an error even though I have been
trying to fix it for hours.
this is the database class
class db {
public $mysqli;
function __construct(){
$mysqli = new mysqli('host', 'user', 'password', 'database');
}
function clean($string){
$string = $mysqli->real_escape_string($string);
}
}
and when I try to call it in a test page like so,
$db = new db();
$db->clean("hi");
I get an error:
Notice: Undefined variable: mysqli in C:\xampp\htdocs\eat\class\db.class.php on line 12
Fatal error: Call to a member function real_escape_string() on a non-object in C:\xampp\htdocs\eat\class\db.class.php on line 12
is there something that I did wrong here? I have been scouring the php manual for a long time and on stackoverflow but it really seems like nobody else is facing this error.
You are using the variable
$mysqliin the method scope. You should use$thisto access object scope.