All Collections
Website
Website Development
How to Connect to the Microsoft SQL Server (ODBC)
How to Connect to the Microsoft SQL Server (ODBC)

Connecting to the Microsoft SQL Server

Updated over a week ago

Step 1 — Check for the /etc/odbcinst.ini File

Check the /etc/odbcinst.ini file and make sure it includes information similar to the one below. You can check this using SSH. Use the command cat /etc/odbcinst.ini for that:

[FreeTDS] 
Description = ODBC for FreeTDS
Driver = /usr/lib64/libtdsodbc.so
Setup = /usr/lib64/libtdsS.so.2
Driver64 = /usr/lib64/libtdsodbc.so
Setup64 = /usr/lib64/libtdsS.so.2
FileUsage = 1

You don’t need to make any changes to this file. Only check if it exists and has data on it. If you cannot find this file, or it’s empty, contact us.

Step 2 — Create a .odbc.ini File

Go to your website's file manager, and select the option to access all the files of your hosting plan:

Accessing the files of the whole hosting plan

This way, you'll land in the root directory of your hosting plan. Here, create a .odbc.ini file:

Creating a new file in the file manager

Add the following text inside the file:

[mssql_odbc] 
Description = MSSQL Server
Driver = FreeTDS
Database = username_databasename
Server = mssql.database.servername.tld
Port = 1433

Replace the following as given below:

  1. username_database — insert the name of the SQL server database to be used

  2. mssql.database.servername.tld — insert the hostname or IP address of the server to connect to

  3. 1433 — insert the port that you'll use for the MSSQL connection; change this number only if your default port isn’t 1433

Save the changes:

Creating a new file in the file manager

Step 3 — Check for the Necessary PHP Extensions

Make sure the following extensions are enabled in your website's PHP configuration settings:

  • odbc

  • pdo_odbc

Step 4 — Test the Connection

Finally, test the connection by using the following line of code:

$dbh = new PDO("odbc:mssql_odbc", "%DB_USER%", "%DB_PASS%");

If everything is set up correctly, the connection will be successful.

What to Do if You're Asked to Install the ODBC Driver

If you get the following error:

This extension requires the Microsoft ODBC Driver for SQL Server. 
Access the following URL to download the ODBC Driver for SQL Server for x64: https://go.microsoft.com/fwlink/?LinkId=163712

It means that your script is not using the PDO function.

At the moment, it’s only possible to make connections to SQL servers using the PDO function for PHP. Other extensions, such as sqlsrv and similar will fail as they do not use .odbc.ini file configurations.

Did this answer your question?