如何使用 php 将 opencart 应用程序连接到 ms sql 服务器数据库?

How to connect an opencart application to an ms sql server database using php?

我正在尝试使用 php 创建一个开放式购物车应用程序,我想将它连接到 sql 服务器。该数据库不是 mysql 数据库。我有以下错误:Call to undefined function DB\mssql_connect()。我这样设置我的配置文件:

<?php
// HTTP
define('HTTP_SERVER', 'http://localhost/restaurant/admin/');
define('HTTP_CATALOG', 'http://localhost/restaurant/');

// HTTPS
define('HTTPS_SERVER', 'http://localhost/restaurant/admin/');
define('HTTPS_CATALOG', 'http://localhost/restaurant/');

// DIR
define('DIR_APPLICATION', 'E:/my work/wamp/www/restaurant/admin/');
define('DIR_SYSTEM', 'E:/my work/wamp/www/restaurant/system/');
define('DIR_IMAGE', 'E:/my work/wamp/www/restaurant/image/');
define('DIR_LANGUAGE', 'E:/my work/wamp/www/restaurant/admin/language/');
define('DIR_TEMPLATE', 'E:/my work/wamp/www/restaurant/admin/view/template/');
define('DIR_CONFIG', 'E:/my work/wamp/www/restaurant/system/config/');
define('DIR_CACHE', 'E:/my work/wamp/www/restaurant/system/storage/cache/');
define('DIR_DOWNLOAD', 'E:/my work/wamp/www/restaurant/system/storage/download/');
define('DIR_LOGS', 'E:/my work/wamp/www/restaurant/system/storage/logs/');
define('DIR_MODIFICATION', 'E:/my work/wamp/www/restaurant/system/storage/modification/');
define('DIR_UPLOAD', 'E:/my work/wamp/www/restaurant/system/storage/upload/');
define('DIR_CATALOG', 'E:/my work/wamp/www/restaurant/catalog/');

// DB
define('DB_DRIVER', 'mssql');
define('DB_HOSTNAME', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'AndlusMarket');
define('DB_PORT', '1433');
define('DB_PREFIX', 'oc_');

我找了很多解决方案,但是很多人说太难了。有人可以帮我吗?

您需要使用 PDO 连接您的 OpenCart

我已经为 mysql 创建了 PDO class 你可以从 OpenCart PDO

下载

你需要把这个 class 放在

{your opencart folder} > system >database > pdo.php

创建 class 文件名 pdo.php

只需将 class 名称替换为 DBpdo

只需替换class中的字符串

$this->params->connstr = "sqlsrv:Server={$host};dbname={$name};charset={$charset}";

并且您需要更改 config.php

define('DB_DRIVER', 'PDO');

你可以替换

$this->dbh->exec($this->options['PDO::MYSQL_ATTR_INIT_COMMAND']);

$this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

我认为你应该更新你的问题并针对 opencart 版本。没问题,所以在较新的版本中内置了名为 mpdo 的 class。所以你只需要

define('DB_DRIVER', 'mpdo');
try {
    $this->connection = new \PDO("mysql:host=" . $hostname . ";port=" . $port . ";dbname=" . $database, $username, $password, array(\PDO::ATTR_PERSISTENT => true));
} catch(\PDOException $e) {
    throw new \Exception('Failed to connect to database. Reason: \'' . $e->getMessage() . '\'');
}

$this->connection->exec("SET NAMES 'utf8'");
$this->connection->exec("SET CHARACTER SET utf8");
$this->connection->exec("SET CHARACTER_SET_CONNECTION=utf8");
$this->connection->exec("SET SQL_MODE = ''");

替换为

try {
    $this->connection = new \PDO("sqlsrv:Server=" . $hostname . ";port=" . $port . ";Database=" . $database, $username, $password);
} catch(\PDOException $e) {
    throw new \Exception('Failed to connect to database. Reason: \'' . $e->getMessage() . '\'');
}
$this->connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );