I'm currently dealing with a quite outdated booking calendar called GuestCal, which is no longer supported but suits my basic needs. Unfortunately, my web host made it mandatory to switch to PHP 8 and the PHP script is no longer working correctly.
The fatal error which I receive is:
Fatal error: Uncaught TypeError: mysqli_query(): Argument #1 ($mysql) must be of type mysqli, bool given in \guestcal\includes\db.class.php:134
I think the issue lies within the database connection, as the mysqli_query receives a false
instead of a mysqli object - as you can see down below.
public function select ($query) {
$query = $this -> queryAddTablePrefixes ($query);
$result = mysqli_query($this->link, $query);
if (!$result) {
if (DEBUG)
trigger_error (mysqli_error($this->link));
return false;
}
else {
$num = mysqli_num_rows($result);
if ($num) {
$array = array ();
while ($row = mysqli_fetch_assoc($result))
$array[] = $row;
return $array;
}
else
return false;
}
}
The class for the db connection is defined in class DatabaseConnection
with help of a wrapper.
/**
* Wrapper for connect ()
* @param $db Array with vars from config
* @return result from connect ()
*/
public function databaseConnection ($mysql) {
$this->config = $mysql;
return $this->connect ();
}
/**
* Tries to connect to db and checks installation.
* @return true if no error, else false
*/
public function connect () {
// Reset $error
$this->error = false;
// Try to connect to db
$this->link = new mysqli($this->config['host'], $this->config['user'], $this->config['pass']);
//$this->link = mysqli_connect('localhost', 'root', '');
if (!$this->link)
$this->error = 1; // Connection failed.
else {
if (function_exists ('mysql_set_charset'))
mysqli_set_charset($this->link,'utf8');
// Try to select db
if (mysqli_select_db($this->link,$this->config['name'])) {
// Look for prefs table
$version = $this->select ("SELECT `value` FROM `prefs` WHERE `name`='dbVersion'");
if (!$version) {
// Check for v1.x installation with this year's table
$tables = $this->select ("SHOW TABLES");
if ($tables) {
foreach ($tables as $table) {
if (in_array ($this->config['pfix'] . date ('Y'), $table)) {
$this->error = 5; // v1.x installation found
break;
}
}
}
if (!$this->error)
$this->error = 3; // Table does not exist
}
// Check for db version
else {
$this->version = $version[0]['value'];
if ($this->version != VERSION)
$this->error = 4; // DB-Version and Files-Version differ
}
}
else
$this->error = 2; // Database does not exist
}
if ($this->error)
return false;
else
return true;
}
I tried several things to establish a successful connection, e.g. the snippet $mysqli_connection = new mysqli('host', 'user', 'password', 'dbname');
works without any problems in my config.inc.php, where all db-related information are stored.
Maybe the problems has something to do when referencing the object via $this
? Thanks in advance for your help!