【问题标题】:open cart displaying Deprecated: mysql_connect() message on top打开购物车,顶部显示已弃用:mysql_connect() 消息
【发布时间】:2013-11-08 13:26:54
【问题描述】:

我在本地服务器上安装了打开的购物车,但它在顶部显示一条消息。

已弃用:mysql_connect():mysql 扩展已弃用,将来将被删除:在第 6 行的 D:\new\htdocs\business\system\database\mysql.php 中使用 mysqli 或 PDO 代替

我该如何解决?

【问题讨论】:

  • 简单的解决方法是在 php.ini 中设置 display_errors=0。另一种方法是在您的脚本中执行error_reporting(0);。但是你应该知道 mysql_* 函数不应该再使用了:stackoverflow.com/questions/12859942/…

标签: php mysql pdo mysqli opencart


【解决方案1】:

此错误是因为您使用的是 PHP 5.5 或更高版本。最好的解决方案是不要像其他人所说的那样抑制错误(因为这会阻止您看到其他问题的错误),而是为 OpenCart 安装 mysqli 扩展/PDO 扩展。 This one 是免费的并且运行良好 - 这是我使用的一个

【讨论】:

  • 虽然一个简单的解决方案是设置 display_errors=0,但安装扩展非常简单,当然更好。复制文件并替换 2 行简单的代码。杰伊的好建议。谢谢。
  • @jay 您提供的链接已失效。旁边有链接吗?问候。
【解决方案2】:

我在 ApacheFriends XAMPP 版本 1.8.3 上使用 opencart-1.5.6.1.zip,我也在每个页面上都看到此错误消息。

打开 opencart/config.php 和 opencart/admin/config.php。

编辑'mysql' -> 'mysqli'

例如

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

保存文件,无需重新启动任何东西。错误信息消失了。

【讨论】:

    【解决方案3】:

    以下是opencart 1.5.6或中PDO的代码

        <?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();
        }
    }
    

    【讨论】:

    • 如果你切换到新版本的 opencart 那么这个类将对你有用,因为在新版本中 /upload/system/library/db/ 中的 mpdo.php 中有内置类
    猜你喜欢
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多