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 https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -

 
 curl https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-2017.list | sudo tee /etc/apt/sources.list.d/mssql-server-2017.list

 
 curl https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-2017.list | 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 https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
exit
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 extension=pdo_sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/30-pdo_sqlsrv.ini
echo extension=sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/20-sqlsrv.ini
exit

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';
    sqlsrv_free_stmt($getResults);

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

Leave a Reply

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

RSS
Follow by Email
Facebook
Twitter