How to connect opencart application to sql server database using php

Hi guys, I'm trying to create an application for an open recycle bin using php, and I want to link it to a SQL Server database, not a mysql database, and I got the following error: Call to undefined function DB\mssql_connect() , and I installed my configuration file, for example 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 alot for a solution, but many said it was too complicated. Can anybody help me?

+5
source share
2 answers

I think you should update your question and be specific to the opencart version. No problem, so the new version has a built-in class called mpdo. so you just need

 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

 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
source

You need to use PDO to connect OpenCart

I created a PDO class for mysql that you can download from OpenCart PDO

you need to put this class in

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

create class file name using pdo.php

Just replace the class name with DBpdo

Just replace the line in the class

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

and you need to change it in config.php

 define('DB_DRIVER', 'PDO'); 

you can replace

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

with

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

All Articles