Using PHP for MSSql Server

With php 7, I have always though of how fast it will be to run MSSql instead of My Sql. It turns out to be too fast. With MSSql express edition free and able to run on linux and Mac, It can be a good alternative to MySql.

Following are the steps to run MSSql in core php 7.

You can follow the official site of microsoft for the PDO Sql drivers. You can use it for mac, ubuntu, Rhel, Debian. I have used ubuntu in the following blog.

Step 1. Install MSSql Server


 curl | sudo apt-key add -

 curl | sudo tee /etc/apt/sources.list.d/mssql-server-2017.list

 curl | sudo tee /etc/apt/sources.list.d/mssql-server-2017.list

sudo apt-get update
sudo apt-get install mssql-server


Step 2. SetUP MSSql Server

sudo /opt/mssql/bin/mssql-conf setup

Step 3. Install the ODBC Driver and SQL Command Line Utility for SQL Server

sudo su
curl | apt-key add -
curl > /etc/apt/sources.list.d/mssql-release.list
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install msodbcsql17 mssql-tools
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
sudo apt-get install unixodbc-dev

Step 4. Checking the SQL Server with sql command line toold

sqlcmd -S localhost -U sa -P yourpassword
The output of the following command will be .
1> # you can run your queries here

Step 5 . Install the PHP Driver for SQL Server

sudo pecl install sqlsrv
sudo pecl install pdo_sqlsrv
sudo su
echo >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/30-pdo_sqlsrv.ini
echo >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/20-sqlsrv.ini

Step 6. Now connect to the SQL using PHP and fetch some records


    $time_end = microtime(true);
    $execution_time = round((($time_end - $time_start)*1000),2);
    echo 'QueryTime: '.$execution_time.' ms';

Voila !! and now you can run any operations using your MSSQL and PHP 7.

1 thought on “Using PHP for MSSql Server

  1. You completed a number of good points there. I did a search on the subject matter and found most persons will go along with with your blog.

Leave a Reply

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

Follow by Email