【问题标题】:How to connect an opencart application to an ms sql server database using php?如何使用 php 将 opencart 应用程序连接到 ms sql server 数据库?
【发布时间】:2016-08-23 05:41:53
【问题描述】:

我正在尝试使用 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_');

我搜索了很多解决方案,但很多人说这太难了。谁能帮帮我?

【问题讨论】:

标签: php mysql sql-server opencart


【解决方案1】:

我认为您应该更新您的问题并具体针对 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 );

【讨论】:

  • 太好了,它给了我异常无法连接到数据库
【解决方案2】:

您需要使用 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!它给了我那个错误?
  • 你需要给类名DBpdo和define('DB_DRIVER', 'pdo');
  • 你需要给类名 DBpdo 是什么意思?
  • 是的,你需要用 db.php 中的 DBpdo 更新类名,它包含 DB 前缀
  • 它给了我这个错误:在索引中找不到类'DBpdo'
猜你喜欢
  • 2021-10-09
  • 2018-05-16
  • 2013-06-01
  • 2017-06-10
  • 1970-01-01
  • 2020-03-07
  • 2018-07-02
  • 2018-09-03
  • 1970-01-01
相关资源
最近更新 更多