0 votes
980 views
by (1.5k points)

After upgrading PHP on host, I am troubled by getting following error message on OpenCart powered ecommerce website.

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/user/public_html/system/database/mysql.php on line 6

1 Answer

0 votes
by (10.3k points)

You might be using the old version of OpenCart (i.e. 1.5.x). As old OpenCart lacks support for newer MySQLi drivers. You can manually add the drivers to clean above error.

  1. Go to /public_html/system/database
  2. Create file mmysqli.php
  3. Copy the following code into above mmysqli.php or alternatively from GitHub
    <?php
    final class mMySQLi {
        private $link;
    
        public function __construct($hostname, $username, $password, $database) {
            $this->link = new mysqli($hostname, $username, $password, $database);
    
            if ($this->link->connect_error) {
                trigger_error('Error: Could not make a database link (' . $this->link->connect_errno . ') ' . $this->link->connect_error);
            }
    
            $this->link->set_charset("utf8");
            $this->link->query("SET SQL_MODE = ''");
        }
    
        public function query($sql) {
            $query = $this->link->query($sql);
    
            if (!$this->link->errno) {
                if ($query instanceof mysqli_result) {
                    $data = array();
    
                    while ($row = $query->fetch_assoc()) {
                        $data[] = $row;
                    }
    
                    $result = new stdClass();
                    $result->row = isset($data[0]) ? $data[0] : array();
                    $result->rows = $data;
                    $result->num_rows = $query->num_rows;
    
                    $query->close();
    
                    return $result;
                } else {
                    return true;
                }
            } else {
                trigger_error('Error: ' . $this->link->error  . '<br />Error No: ' . $this->link->errno . '<br />' . $sql);
            }
        }
    
        public function escape($value) {
            return $this->link->real_escape_string($value);
        }
    
        public function countAffected() {
            return $this->link->affected_rows;
        }
    
        public function getLastId() {
            return $this->link->insert_id;
        }
    
        public function __destruct() {
            $this->link->close();
        }
    }
    ?>
  4. Edit config.php from below two paths.
    /public_html
    /public_html/admin

  5. In both directories look for the following line in config.php

    define('DB_DRIVER', 'mysqli');
  6. Replace it with

    define('DB_DRIVER', 'mmysqli');
  7. Save change in both directories

  8. Done..!

Alternatively, there is an extension to do so. for this visit here.

...