I have read almost all references here about this topic. My individual problem is that I have an OOP CMS which had been coded using the long since deprecated mysql database access. Now my task is to upgrade everything from PHP 5.6 to PHP 7.x. The choice was either PDO or mysqli and I have decided for the latter since its syntax comes nearer to my code reading and understanding.
Here is my mysql code:
Credentials are defined in DBSettings.php:
<?php
// Variablen fuer den Datenbankzugriff
$DATABASESERVER = 'localhost';
$DATABASENAME = 'whatever_db';
$DATABASEUSERNAME = 'blithering_idiot';
$DATABASEPASSWORD = 'moronic_fool_1234_$%&/';
$DATABASETYPE = 'MySQL';
//require_once($_SERVER['DOCUMENT_ROOT'].'/DBSettings.inc.php');
?>
A file called PHP5_classDatabase.php contains the class:
<?php
error_reporting(-1);
//ini_set('display_errors', 'On');
class database extends object
{
private $rConnection;
private $sDatabaseType;
private $sDatabaseServer;
private $sDatabaseName;
private $sUserName;
private $sPassword;
public function __construct($sDatabaseServer, $sUserName, $sPassword, $sDatabaseType = 'MySQL')
{
switch ($sDatabaseType) {
case 'MySQL':
$this->setConnection(mysqli_connect($sDatabaseServer, $sUserName, $sPassword));
$this->setDatabaseType($sDatabaseType);
break;
case 'MSSQL':
$this->setConnection(mssql_connect($sDatabaseServer, $sUserName, $sPassword));
$this->setDatabaseType($sDatabaseType);
break;
default:
$this->setConnection(mysqli_connect($sDatabaseServer, $sUserName, $sPassword));
$this->setDatabaseType('MySQL');
}
$this->setDatabaseServer($sDatabaseServer);
$this->setUserName($sUserName);
$this->setPassword($sPassword);
$this->setDatabaseName('');
}
public function setConnection($rConnection)
{
$this->rConnection = $rConnection;
}
public function setDatabaseType($sDatabaseType)
{
$this->sDatabaseType = $sDatabaseType;
}
public function setDatabaseServer($sDatabaseServer)
{
$this->sDatabaseServer = $sDatabaseServer;
}
public function setDatabaseName($sDatabaseName)
{
$this->sDatabaseName = $sDatabaseName;
}
public function setUserName($sUserName)
{
$this->sUserName = $sUserName;
}
public function setPassword($sPassword)
{
$this->sPassword = $sPassword;
}
public function getConnection()
{
return ($this->rConnection);
}
public function getDatabaseType()
{
return ($this->sDatabaseType);
}
public function getDatabaseServer()
{
return ($this->sDatabaseServer);
}
public function getDatabaseName()
{
return ($this->sDatabaseName);
}
public function getUserName()
{
return ($this->sUserName);
}
public function getPassword()
{
return ($this->sPassword);
}
public function selectDatabase($sDatabaseName)
{
switch ($this->getDatabaseType()) {
case 'MySQL':
mysqli_select_db($sDatabaseName);
break;
case 'MSSQL':
mssql_select_db($sDatabaseName);
break;
default:
mysql_select_db($sDatabaseName);
}
$this->setDatabaseName = $sDatabaseName;
}
}
?>
This file contains much more database-specific stuff but without meaning for this principal access description.
Now comes the last part, the file functionsDatabase.php in which the function "connectToDB()" is being taken care of:
<?php
function connectToDB() {
global $DATABASESERVER, $DATABASEUSERNAME, $DATABASEPASSWORD, $DATABASENAME, $oDatabase;
$oDatabase = new database($DATABASESERVER, $DATABASEUSERNAME, $DATABASEPASSWORD);
$oDatabase->selectDatabase($DATABASENAME);
}
[... functions with database association, not important here ...]
?>
The connectToDB();
is then executed by a file called general.php which is referenced to by any other PHP script within this CMS.
This code has, so far, worked flawlessly, and the subsequent tons of database save and read and update and delete procedures worked just fine.
Now, I have tried several times to switch that code to mysqli and I do know the theory about its native object-oriented and procedural construction as to be read from, for instance, w3school.
My way to change things into mysqli has looked like this:
In PHP5_classDatabase.php I changed the tp the following code (display only of the changed context):
[...]
public function __construct($sDatabaseServer, $sUserName, $sPassword, $sDatabaseType = 'MySQL', $sDatabaseName = 'adipositas')
{
switch ($sDatabaseType) {
case 'MySQL':
//Change 1: $this->setConnection(mysqli_connect($sDatabaseServer, $sUserName, $sPassword, $sDatabaseName));
//Change 2: $this->db = mysqli_connect($sDatabaseServer, $sUserName, $sPassword, $sDatabaseName);
$db = mysqli_connect($sDatabaseServer, $sUserName, $sPassword, $sDatabaseName);
$this->setDatabaseType($sDatabaseType);
break;
[...]
public function selectDatabase($sDatabaseName = 'adipositas')
{
switch ($this->getDatabaseType()) {
case 'MySQL':
mysqli_select_db($this->$db, $sDatabaseName);
break;
[...]
}
And in functionsDatabase.php I made the following changes:
[...]
function connectToDB() {
global $DATABASESERVER, $DATABASEUSERNAME, $DATABASEPASSWORD, $DATABASENAME, $oDatabase;
$oDatabase = new database($DATABASESERVER, $DATABASEUSERNAME, $DATABASEPASSWORD, $DATABASENAME);
$oDatabase->selectDatabase($DATABASENAME);
}
Using XDebug, I get various error messages depending on my play with how to change the potential access to the database. In the described change, they are as such:
Notice: Undefined variable: db in C:\wamp64\apps\adipositas\resources\PHP5_classDatabase.php on line 167
... referring to:
mysqli_select_db($this->$db, $sDatabaseName);
... and ...
Fatal error: Cannot access empty property in C:\wamp64\apps\adipositas\resources\PHP5_classDatabase.php on line 167
... referring to the same line, of course.
Has anybody an idea how to handle this or where my error is?