How to connect to MS SQL Server using PHP
Today we will read how to connect to MS SQL Server using PHP. Normally PHP is bundled in an package having MySql as database all required dlls, extensions and libraries to connect to MySql Database with PHP. When we want to make connection with MS-Sql server we need few extra things like some extensions and a bit different code to connect.
Things required to connect to MS SQL SERVER
- Download required dlls
- Enable extensions
- (Optional) Install MS Sql Server CLI if SQL Server is installed on different PC.
- Restart WAMP
- Test PHP code to connect to MS Sql
1- Download required dlls to connect to MS SQL Server using PHP
For windows operating systems you can download required dlls from the following link
https://www.microsoft.com/en-us/download/details.aspx?id=20098
for PHP 7.4 use the following Dlls
for PHP 5.2 > download sqlsrv40.exe from above link.
Run this exe it will ask for folder location to extract dll file.
after completing goto that folder and copy required dlls to php extension folder.
Copy required dlls to PHP extension folder
if you are using php 5.3 copy 4 dll files ending with 54dll
if you are using php 5.6 copy 4 dll files ending with 56dll
and paste in PHP ext folder for me it is C:\wamp64\bin\php\php5.6.35\ext
I have copied the following files
- php_pdo_sqlsrv_56_nts.dll
- php_pdo_sqlsrv_56_ts.dll
- php_sqlsrv_56_nts.dll
- php_sqlsrv_56_ts.dll
2- Enable extensions
Goto php.ini file and add the following lines Better to add after other extension entries
1 2 3 4 |
Extension=php_sqlsrv_56_ts.dll Extension=php_sqlsrv_56_nts.dll Extension=php_pdo_sqlsrv_56_ts.dll Extension=php_pdo_sqlsrv_56_nts.dll |
3- Install MS Sql Server CLI
If MS Sql Server is installed on the same PC where PHP is installed than this is not required, but if the PC where PHP is installed has no MS SQL server than it will need MS-SQL-SERVER-CLI to operate sql server related commands. for windows operating systems you can download it from the following link, and its installation is easy just few clicks.
https://download.microsoft.com/download/B/E/D/BED73AAC-3C8A-43F5-AF4F-EB4FEA6C8F3A/ENU/x64/sqlncli.msi
4- Restart WAMP
Restart WAMP server, (All Services). it will be better to exit wamp and restart again. For me it was not working with simple restating all services
After restarting we are all done.
5- Test PHP code to connect to MS Sql
TO connect to MS SQL Server create a PHP file and pate the following code, and replace ip, DatabaseName, UserName and YourPassword
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
$cinfo = array( "Database" => 'employees_database', "UID" => '192.168.11.12', "PWD" => 'my_password' ); $conn = sqlsrv_connect(v18.v15,$cinfo); if( $conn === false ) { echo "ERROR: (sqlsrv_connect): ".print_r(sqlsrv_errors(), true); exit; } $sql = "select top 10 * from employees"; $stmt = sqlsrv_query($conn, $sql ); if( $stmt === false ) { return "ERROR (sqlsrv_query): ".debug(sqlsrv_errors()); } echo '<table>'; while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { echo '<tr>'; echo '<td>' . $row['employee_name'] . '</td>'; echo '<td>' . $row['employee_email'] . '</td>'; echo '</tr>'; } echo '</table>'; sqlsrv_free_stmt($stmt); sqlsrv_close($conn); |
Here I will put code snippet for cakephp to MSSql-Server connection also
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/* connect to MS SQL Server using Cake PHP tested with cakephp 2.x using php5 and 7 by Alam */ public $sqlsrv = array( 'datasource' => 'Database/Sqlserver', 'persistent' => false, 'host' => '192.168.0.2', 'login' => 'UserName', 'password' => 'YourPassword', 'database' => 'DatabaseName', 'prefix' => '', //'encoding' => 'utf8', ); |
this is all done, hope this was helpful, write you experience in comments below.
Comments