OpenCart – PHP Basics Part 2 (DB CONNECT)

Your Opencart store is built upon an MVCL framework. The framework is useless without a Database to access. The Database holds all of the product, store, order, and customer information along with localisation characteristics such as language and currency. Most of these parameters are held in a setting string. This article covers the basics in how Opencart connects to the database with PHP. Opencart uses a couple files to take care of the connection.

First, let’s give you a classic barebones example of a PHP DB CONNECT:

<?php
// Create connection
$con=mysqli_connect(“example.com”,”peter”,”abc123″,”my_db”);

// Check connection
if (mysqli_connect_errno($con))
  {
  echo “Failed to connect to MySQL: ” . mysqli_connect_error();
  }
?>

This will look a little different in Opencart framework because of the use of classes. When you install Opencart, on the install page you will fill in the DB NAME, DB USERNAME, DB PASSWORD, and a PREFIX if any. This makes it easy on you the admin instead of having to go into the configuration files. However, let’s see where the install writes these credentials.

Open the file: /config.php and /admin/config.php and look towards the bottom. You will see the following code:

// DB

define(‘DB_DRIVER’, ‘mysql’);

define(‘DB_HOSTNAME’, ‘localhost’);

define(‘DB_USERNAME’, ‘xxxxxx’);

define(‘DB_PASSWORD’, ‘xxxxxx’);

define(‘DB_DATABASE’, ‘xxxxxx’);

define(‘DB_PREFIX’, ”);

Whatever you specified in your install will show up here. The x’s are just there to hide my own details :) This is considered super sensitive data and you NEVER want to share this with anyone except trusted folks.

CONNECTING TO THE DB

Open /index.php and look for this code:

// Database
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$registry->set(‘db’, $db);

Notice that the constants defined here are the same in the config.php. This is the initial DB CONNECT procedure. It executes on and before every other procedure because it must.  Notice it is calling the DB class new DB()

Now open system/library/db.php and notice this function:

public function __construct($driver, $hostname, $username, $password, $database) {
        if (file_exists(DIR_DATABASE . $driver . ‘.php’)) {
            require_once(DIR_DATABASE . $driver . ‘.php’);
        } else {
            exit(‘Error: Could not load database file ‘ . $driver . ‘!’);
        }
                
        $this->driver = new $driver($hostname, $username, $password, $database);
    }

Now you know how Opencart connects to a MYSQL DATABASE!

Looking for quality OpenCart Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , | Posted under OpenCart | RSS 2.0

Author Spotlight

Joe Stenhouse

Joe Stenhouse

I am a web application developer that specializes in PHP, JAVASCRIPT, MYSQL, HTML, and CSS. We manifest exciting potentials within the world wide web through means of innovation.

Leave a Reply

Your email address will not be published. Required fields are marked *


2 + 4 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>