PHP mssql drivers


title: “PHP mssql drivers”
date: 2019-02-18T19:33:36
slug: php-mssql-drivers


Step 2.1 Install the PHP Driver for SQL Server

If using PHP 7.3, replace sqlsrv and pdo_sqlsrv in the following commands with sqlsrv-5.4.0preview and pdo_sqlsrv-5.4.0preview or later, as earlier versions are not compatible with PHP 7.3.

Terminal


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
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

Copy

Step 2.2 Create a database for your application

Create the database using sqlcmd

Terminal


sqlcmd -S localhost -U sa -P your\_password -Q "CREATE DATABASE SampleDB;"

Copy

Step 2.3 Create a PHP app that connects to SQL Server and executes queries

Terminal


mkdir SqlServerSample
cd SqlServerSample

Copy

Using your favorite text editor, create a new file called connect.php in the SqlServerSample folder. Paste the code below inside into the new file.

PHP

<?php
 $serverName = "localhost";
 $connectionOptions = array(
 "Database" => "SampleDB",
 "Uid" => "sa",
 "PWD" => "your\_password"
 );
 //Establishes the connection
 $conn = sqlsrv\_connect($serverName, $connectionOptions);
 if($conn)
 echo "Connected!"
?>
Print Friendly, PDF & Email