---
title: "Accessing SQL server database with PHP"  
description: "The Microsoft SQL Server 2005 Driver for PHP allows PHP developers to access data in SQL Server databases. The driver includes support for Windows and"  
author: "Anonymous User"  
published: 2012-03-12  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/287/accessing-sql-server-database-with-php  
category: "php"  
tags: ["php"]  
reading_time: 2 minutes  

---

# Accessing SQL server database with PHP

The [Microsoft SQL](https://www.mindstick.com/forum/159597/my-sql-server-vs-microsoft-sql-server-which-one-better) Server 2005 Driver for PHP allows PHP developers to access [data in SQL](https://www.mindstick.com/articles/12937/how-to-import-excel-data-in-sql-server-2014) [Server databases](https://www.mindstick.com/forum/158359/what-are-some-common-techniques-for-backing-up-and-restoring-sql-server-databases). The driver includes support for Windows and SQL Server [Authentication methods](https://www.mindstick.com/forum/160407/how-does-a-bearer-token-differ-from-other-authentication-methods-such-as-username-and-password), transactions, parameter binding, streaming, metadata access, [connection pooling](https://www.mindstick.com/articles/804/connection-pooling-in-ado-dot-net), and [error handling](https://www.mindstick.com/forum/160168/error-handling-in-go).\

To [access the data](https://answers.mindstick.com/qa/40056/what-is-the-weo-update-and-how-can-i-access-the-data) from SQL [server database](https://www.mindstick.com/forum/155643/how-to-create-sql-server-database-in-google-cloud) you have to perform some changes in php.ini file.

**Change 1:** Put the extension file (php_sqlsrv.dll or php_sqlsrv_ts.dll) in the PHP extension directory. If you are running the non-thread-safe version of PHP (php5.dll), you should use the non-thread-safe version of the driver (php_sqlsrv.dll).

**Change 2:** Uncomment the extension=php_sqlsrv.dll or php_pdo_mssql.dll in php.ini file.

After performing these changes [restart your](https://answers.mindstick.com/qa/50482/how-to-force-restart-your-iphone-x-hard-reset) web server.

##### Example:

Here I’m creating an example for connecting PHP script with SQL server. In this example, simply I’m retrieving data from MS-SQL table and print it.

```
<?php               // SQL server connection string information                $serverName = "XXXX";                $userName = "XXXX";                $password = "XXXX";                $db_Name = "MyDatabase";                // connect with SQL server database                $conn = mssql_connect($serverName, $userName, $password)                        or die ("Database cannot connected successfully");           // Select SQL database                $selectDB = mssql_select_db($db_Name, $conn)                            or die("Database cannot be opended");                 // SQL server query                $qry  = "select Name, ContactNo, EmailId from tblLogin";                // Execute Query                $qry_result = mssql_query($qry);      // exatract table value                while($row = mssql_fetch_array($qry_result))                {                    // print table value                 echo $row['Name'].'\t\t';                     echo $row['ContactNo'].'\t\t';                     echo $row['EmailId'];              }                // Close connection with SQL server database                mssql_close();        ?>
```

---

Original Source: https://www.mindstick.com/blog/287/accessing-sql-server-database-with-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
