如何使用PHP将Opencart应用程序连接到MS SQL Server数据库?

4

我正在尝试使用PHP制作一个OpenCart应用程序,并且想要将其连接到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_');

我为这个问题搜索了很多解决办法,但很多人都说这太难了。有没有人能帮帮我?


PHP的哪个版本?mssql_*函数已从PHP 7中移除。您应该使用sqlsrv_*函数来连接到SQL Server - ImClarky
php 5.1.12 @ImClarky 这是我正在使用的版本。 - Mohamed Elbiheiry
请试一试:http://stackoverflow.com/questions/16558056/how-to-connect-opencart-with-mssql-server - Jenson M John
2个回答

2

我认为您应该更新问题并明确指出opencart版本。在较新的版本中,有一个名为mpdo的内置类,因此您只需要使用它即可。

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 );

很好,它给了我一个异常,无法连接到数据库。 - Mohamed Elbiheiry

0

你需要使用PDO来连接你的OpenCart

我已经为MySQL创建了PDO类,你可以从OpenCart PDO下载。

你需要将这个类放在

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

创建名为pdo.php的类文件

只需将类名替换为DBpdo

只需替换类中的字符串

$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 );

无法加载数据库驱动程序PDO!它给我报错了吗? - Mohamed Elbiheiry
你的意思是需要给类命名为DBpdo吗? - Mohamed Elbiheiry
你是否已经创建了以"pdo"命名的类文件,并将类名命名为"Dbpdo"? - Pranav Bhatt
2.2.0编译版,这就是版本。 - Mohamed Elbiheiry
让我们在聊天中继续这个讨论 - Mohamed Elbiheiry
显示剩余13条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接