简体   繁体   English

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

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

I am trying to make an open cart application using php, and I want to connect it a sql server.我正在尝试使用 php 制作一个开放式购物车应用程序,我想将它连接到一个 sql 服务器。 The database is not mysql database.数据库不是mysql数据库。 I have the following error: Call to undefined function DB\\mssql_connect() .我有以下错误: Call to undefined function DB\\mssql_connect() I set my config file like that:我这样设置我的配置文件:

<?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_');

I searched a lot for a solution, but many people said it is too difficult.我搜索了很多解决方案,但很多人说这太难了。 Can anyone help me with it?任何人都可以帮助我吗?

I think you should update your question and be specific for the opencart version.我认为您应该更新您的问题并针对 opencart 版本。 No problem all right so in newer version there is built in class named mpdo.没问题,所以在较新的版本中,内置了名为 mpdo 的类。 so you need to just所以你只需要

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 = ''");

Replace it with将其替换为

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

You need to use PDO to connect your OpenCart您需要使用 PDO 来连接您的 OpenCart

I have created PDO class for mysql you can download from OpenCart PDO我已经为 mysql 创建了 PDO 类,您可以从OpenCart PDO下载

you need to put this class in你需要把这个类放在

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

create class file name with pdo.php使用 pdo.php 创建类文件名

Just replace class name with DBpdo只需用 DBpdo 替换类名

Just replace the string in class只需替换类中的字符串

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

and you need to change in your config.php你需要改变你的 config.php

define('DB_DRIVER', 'PDO');

you could replace你可以更换

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

with

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM