This is the documentation for concrete5 version 5.6 and earlier. View Current Documentation

The requirement I had was to connect Concrete5 to an external MSSQL Database.

My development server with Concrete5 (v5.4.1.1) on it runs Apache v2.0.55 with PHP v5.2.6 on Windows XP SP2.

The server I setup with the external MSSQL Database on it runs on Windows Server 2008 R2 with MS SQL Server 2008 R2 (nice naming convention ey!)

First of all you will need to setup the MS SQL Server yourself, I won't be taking you through these steps. You must have this setup already.

Second you need to setup Apache to use the MSSQL extension. With PHP 5.2.6 I had issues because the driver it was shipped with didn't work (a known issue). You will need to download the latest version of PHP and grab the correct driver if you also use this version. The driver you want is "ntwdblib.dll", copy this to the PHP directory root and the Apache bin directory. In your php.ini file add or uncomment "extension=php_mssql.dll". Restart Apache and test the installation using the phpinfo function.

Now for the Concrete5 hacking!

If you want to use MSSQL instead of MySQL by default that's easy enough to change the switch but you may need to change a lot of other code (good luck with that). Just open concrete/config/base.php and find "DB_TYPE" on line 452, then change "define('DB_TYPE', 'mysql');" to "define('DB_TYPE', 'mssql');"

Okay now to keep MySQL as the default and to use MSSQL or some other database whenever you please follow these steps.

1) Open concrete/libraries/loader.php, back up the file, you will need to hack the file as you can't simply override the loader.php file like other files (at least I couldn't) 2) Find the db function, should be line 181

Add a new parameter to the function definition, $dbType with default to mysql.

public function db($server = null, $username = null, $password = null, $database = null, $create = false, $autoconnect = true, $dbType='mysql') {

Change lines 184 to 188 to:

if ($server == null && defined('DB_SERVER')) {  
if($dbType != '') {
    $dsn = $dbType . '://' . DB_USERNAME . ':' . rawurlencode(DB_PASSWORD) . '@' . rawurlencode(DB_SERVER) . '/' . DB_DATABASE;
} else {
    $dsn = DB_TYPE . '://' . DB_USERNAME . ':' . rawurlencode(DB_PASSWORD) . '@' . rawurlencode(DB_SERVER) . '/' . DB_DATABASE;
}
} else if ($server) {
if($dbType != '') {
    $dsn = $dbType . '://' . $username . ':' . rawurlencode($password) . '@' . rawurlencode($server) . '/' . $database;
} else {
    $dsn = DB_TYPE . '://' . $username . ':' . rawurlencode($password) . '@' . rawurlencode($server) . '/' . $database;
}
}

Wrap the if statement that says "if (DB_CHARSET != '') {" in another if statement like this:

if($dbType == 'mysql') {
if (DB_CHARSET != '') {

Now that we have hacked Concrete5 lets create a test page to make sure it all works.

1) Go to your concrete5 root directory, create a new single php file under single_pages called my_test.php 2) Go to the dashboard of Concrete5, click Pages and Themes from the left side navigation, then click the Single Pages tab. Scroll down to the bottom of the page, enter "my_test" into the text field to add a new single page, click the add button. Now find the page in the list and click it to go to it. 3) Open your my_test.php file in your editor of choice 4) Enter the following code:

$db = Loader::db( 'HOST', 'USER', 'PASS', 'DBNAME', true, true, 'mssql');
$r = $db->Execute('select * from testtable');
while ($row = $r->FetchRow()) {
var_dump($row);
}

Replace "testtable" with whatever table you have in your MSSQL (or other) database. This is the standard code you use to connect to another MySQL database. All we have done is passed another parameter to tell ADODB to use MSSQL. This should work with any other database support by ADODB.

To revert back to MySQL add the following code:

$db = Loader::db(null, null, null, null, true);
$r = $db->Execute('select * from PageTypes');
while ($row = $r->FetchRow()) {
var_dump($row);
}

This will grab the info from PageTypes that's standard with Concrete5's MySQL database.

And that should be it. Good luck!

Loading Conversation