0

I installed open cart on my local server, but it is displaying a message at top.

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in D:\new\htdocs\business\system\database\mysql.php on line 6

How can I fix it ?

RDAxRoadkill
  • 414
  • 5
  • 24
BigTech
  • 460
  • 6
  • 11
  • 22
  • simple solution is to set display_errors=0 in php.ini. Another way is to do `error_reporting(0);` in your script. But you should know that mysql_* functions should not be used any more: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – steven Oct 29 '13 at 11:57

3 Answers3

9

This error is because you're using PHP 5.5 or above. The best solution to this is to not suppress errors as others have said (since that prevents you seeing errors from other issues) but to install a mysqli extension/PDO extension for OpenCart. This one is free and works well - it's the one I use

Jay Gilford
  • 15,141
  • 5
  • 37
  • 56
  • While a simple solution would be to set display_errors=0 it is pretty simple just to install the extension and of course better. Copy files and replace 2 simple lines of code. Excellent suggestion Jay. Thanks. – Observer Jan 07 '14 at 08:58
  • @jay the link you provide is dead. Is there any link beside that? Regards. – Fiido93 Sep 28 '15 at 04:38
1

I am using opencart-1.5.6.1.zip on ApacheFriends XAMPP Version 1.8.3 and I see this error message on every page too.

Open opencart/config.php and opencart/admin/config.php.

Edit 'mysql' -> 'mysqli'

e.g.

//define('DB_DRIVER', 'mysql');
define('DB_DRIVER', 'mysqli');

Save the files and no need to restart anything. The error message is gone.

oraclesoon
  • 799
  • 8
  • 12
0

below is the code for PDO in opencart 1.5.6 or <

    <?php
/**
 * Class for working with database (PDO)
 *
 * @property \PDO $dbh
 *
 * @author      WebImperia Dev
 * @since 0.0.1
 */
final class OC_PDO
{
    /**
     * Link to the database connection
     *
     * @var \PDO
     */
    private $dbh;
    /**
     * List of connection settings
     *
     * @var array
     */
    private $options = array(
        'PDO::ATTR_ERRMODE' => PDO::ERRMODE_SILENT
    );
    /**
     * The number of rows affected by the last operation
     *
     * @var int
     */
    private $affectedRows = 0;
    /**
     * The data for the database connection
     *
     * @var \stdClass
     */
    private $params = array();
    /**
     * Sets the connection and connects to the database
     *
     * @param string $host server Address
     * @param string $user Username
     * @param string $pass Password
     * @param string $name The database name
     * @param string $charset Encoding connection
     */
    public function __construct($host, $user, $pass, $name, $charset = 'utf8')
    {
        $this->params = new stdClass;
        # keep connection data
        $this->params->host    = $host;
        $this->params->user    = $user;
        $this->params->pass    = $pass;
        $this->params->name    = $name;
        $this->params->charset = $charset;
        $this->params->connstr = "mysql:host={$host};dbname={$name};charset={$charset}";
        # add the connection parameters
        $this->options['PDO::MYSQL_ATTR_INIT_COMMAND'] = "SET NAMES '{$charset}'";
        $this->connect();
    }
    /**
     * Connect to database
     */
    public function connect()
    {
        try {
            $this->dbh = new PDO($this->params->connstr, $this->params->user, $this->params->pass, $this->options);
            if (version_compare(PHP_VERSION, '5.3.6', '<=')) {
                $this->dbh->exec($this->options['PDO::MYSQL_ATTR_INIT_COMMAND']);
            }
        } catch (PDOException $exception) {
            trigger_error($exception->getMessage());
        }
    }
    /**
     * Query the database
     *
     * @param string $sql
     * @return \stdClass
     */
    public function query($sql = null)
    {
        if ($this->dbh) {
            $data = new stdClass;

            $sth=$this->dbh->prepare($sql);
            $sth->execute();
            //$sth= $this->dbh->query($sql);
            $this->affectedRows = $sth->rowCount();
            $data->rows         = $sth ? $sth->fetchAll() : array();
            $data->row          = isset($data->rows[0]) ? $data->rows[0] : null;
            $data->num_rows     = $this->affectedRows;
            return $data;
        }
        return null;
    }
    /**
     * Concludes the string in quotation marks to be used in the query
     *
     * @param mixed $string shielded line
     * @return string Returns shielded line or to FALSE , if the driver does not support screening
     */
    public function escape($string = null)
    {
        return $this->dbh ? $this->dbh->quote($string) : null;
    }
    /**
     * Gets the number of rows affected by the last operation
     *
     * @return int
     */
    public function countAffected()
    {
        return $this->affectedRows;
    }
    /**
     * Gets the ID of the last inserted row or sequence of values
     *
     * @return int
     */
    public function getLastId()
    {
        return $this->dbh ? $this->dbh->lastInsertId() : 0;
    }
    /**
     * Gets the name of the driver
     *
     * @return string|null
     */
    public function getDriverName()
    {
        return $this->dbh ? $this->dbh->getAttribute(PDO::ATTR_DRIVER_NAME) : null;
    }
    /**
     * Get information about the version of the client libraries that are used by the PDO driver
     *
     * @return string|null
     */
    public function getVersion()
    {
        return $this->dbh ? $this->dbh->getAttribute(PDO::ATTR_CLIENT_VERSION) : null;
    }
    /**
     * Closing a database connection
     */
    public function close()
    {
        $this->dbh = null;
    }
    public function __destruct()
    {
        $this->close();
    }
}
Pranav Bhatt
  • 715
  • 4
  • 8
  • If you are switched to newer version of opencart then this class will be useful for you because in newer version there is inbuilt class in mpdo.php in /upload/system/library/db/ – Pranav Bhatt Mar 31 '16 at 11:08