简体   繁体   中英

How to convert PHP mysql to mysqli in object-oriented coding?

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?

So the solution is as follows:

Here is my mysqli code:

Credentials are defined in DBSettings.php (remains the same):

    <?php
        // Variablen fuer den Datenbankzugriff
        $DATABASESERVER   = 'localhost';
        $DATABASENAME     = 'whatever_db';
        $DATABASEUSERNAME = 'blithering_idiot';
        $DATABASEPASSWORD = 'moronic_fool_1234_$%&/';
        $DATABASETYPE     = 'MySQL';
    ?>

The constructor in PHP5_classDatabase.php is slightly changed (the lines affected are indicated by *NEW* :

    <?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;
            *NEW* private $db;

            public function __construct($sDatabaseServer, $sUserName, $sPassword, *NEW* $sDatabaseName, $sDatabaseType = 'MySQL')
            {
                switch ($sDatabaseType) {
                    case 'MySQL':
                        *NEW* $db = $this->setConnection(mysqli_connect($sDatabaseServer, $sUserName, $sPassword, $sDatabaseName));
                        $this->setDatabaseType($sDatabaseType);
                    break;
                    case 'MSSQL':
                        $this->setConnection(mssql_connect($sDatabaseServer, $sUserName, $sPassword));
                        $this->setDatabaseType($sDatabaseType);
                    break;
                    default:
                        *NEW* $db = $this->setConnection(mysqli_connect($sDatabaseServer, $sUserName, $sPassword, $sDatabaseName));
                    $this->setDatabaseType('MySQL');
                }
                $this->setDatabaseServer($sDatabaseServer);
                $this->setUserName($sUserName);
                $this->setPassword($sPassword);
                *NEW* this->setDatabaseName($sDatabaseName);
            }

            [...]

            public function selectDatabase(*NEW* $db, $sDatabaseName)
            {
                *NEW* global $sDatabaseServer, $sUserName, $sPassword, $sDatabaseName, $db;

                *NEW* $db = mysqli_connect('localhost', 'root', '', 'adipositas');
                *NEW* if (!$db) {
                    *NEW* echo "Error: Unable to connect to MySQL." . PHP_EOL;
                    *NEW* echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
                    *NEW* echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
                    *NEW* exit;
                *NEW* }

                switch ($this->getDatabaseType()) {
                    case 'MySQL':
                        mysqli_select_db(*NEW* $db, $sDatabaseName);
                    break;
                    case 'MSSQL':
                        mssql_select_db($sDatabaseName);
                    break;
                    default:
                        mysql_select_db(*NEW* $db, $sDatabaseName);
                }
            $this->setDatabaseName = $sDatabaseName;
            }
        }
    ?>

And finally, functionsDatabase.php is changed accordingly:

    function connectToDB()
    {
        global $DATABASESERVER, $DATABASEUSERNAME, $DATABASEPASSWORD, $DATABASENAME, $oDatabase, $db;

        $oDatabase = new database($DATABASESERVER, $DATABASEUSERNAME, $DATABASEPASSWORD, $DATABASENAME);
        $oDatabase->selectDatabase($db, $DATABASENAME);
    }

Now it was only for the adaptation of the mysql components to myslqi. Since there is no mysqli_result() , I took the adaptation from this very good StackOverflow reference .

Most of the exchange work could be done using simple global research and replace modes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM