Apparently My database function can’t fetch the database?
Warning: mysqli::real_escape_string(): Couldn’t fetch database
I’ve created a custom database class which extends MySQLi.
Here’s how I’m constructing it.
public function __construct(){
$db = new mysqli(
DB_HOST, DB_USER, DB_PASS, DB_NAME);
$db->select_db(DB_NAME);
return $db;
}
I am selecting the database, aren’t I?
The code where this is happening is here:
private static function readSession($sessionId)
{
global $database;
$sessionId = session_id();
$sessionId = $database->real_escape_string($sessionId);
$time = time();
$result = $database->query("SELECT sessiondata FROM sessions WHERE sessionid='$sessionId' AND expiry > $time");
if ($result->num_rows > 0) {
$row = $result->fetch_array();
return $row['sessiondata'];
}
return "";
}
The database is being initiated before the Session Management functions are being called.
You say that your custom class extends mysqli. If that is the case, you need to call the mysqli
__construct()in your extending constructor, without returning anything:Extending a class means that you are using a descendent of the parent class. You do not need to instantiate an instance of the parent class, but if you need to make use of its constructor and extend its functionality, you must explicitly call
parent::__construct(...)in the child class’s__construct().