Connect with MSSQL (Microsoft SQL Database)

If you’ve worked with MSSQL databases before you know that this is not a very straight forward connection to the database as it is requiring some first steps before being able to do it so! That’s why we are having a specific tutorial for MSSQL database connection.

Step 1. Installing the correct drivers

So first of all we will need to make sure that we’ve installed correctly some extra drivers into your PHP ini so you can enable the functionality of MSSQL connection. To make the long story short you will need to end up with two extensions:

extension=sqlsrv.so
extension=pdo_sqlsrv.so

Notice: As they are plenty ways to install those drivers and it very depends on the operation system that you are using and the SQL database version for now I am keeping it simple that you will just need to make sure that you will include the above extensions on your php.ini file. At the future this notice will probably be removed and we will replace it with a more specific tutorial.

Step 2. Making sure that you can connect to the MSSQL database with PDO drivers

Before testing to see if Grocery CRUD Enterprise is working as expected for you, you will need to make sure that you can connect successfully with the PDO drivers of MSSQL. In order to do so you will just need to run a test file connection that will look like this:

Once you will see only successful messages and a list of your tables in the database then you are ready to go to step 3

Step 3. Configuring Grocery CRUD Enterprise MSSQL connection:

Your database file for connecting with MSSQL after the installation guide it should look something like this:

//database.php
<?php
return [
    'adapter' => [
        'driver' => 'Pdo',
        'dsn' => 'sqlsrv:server = tcp:my-database.example.com,1433; Database = my_database_name',
        'database' => 'my_database_name',
        'username' => 'my_username',
        'password' => 'F0__787asdjhakdsaASJHSDAk'
    ]
];

Notice: As you may have noticed, I did remove the 'charset' => 'utf8' as it was causing me a weird connection issue. So in case you have a connection issue please also check that you haven’t forgot to remove this line

And this should be it! You will now be able to use all the features that Grocery CRUD Enterprise is offering with MSSQL database.

Controller example with Codeigniter Framework

You can also find a full example of a Codeigniter Framework controller below (please consider that I did hardcode the configurations into the controller just for the shake of the example to be in one file but it is strongly suggested to move those into separate files):