How to connect to SQL Server database through PHP?


I'm building a web-service for an Android application that needs to connect to a SQL Server database. I'm trying to connect through PHP (WAMP) on my home computer to the SQL Server database.

However, I don't have experience SQL Server and don't know how to proceed. For SQL Server, I'm using default settings and Windows Authentication, so I'm not sure what to type into the connection string.

Below you have my connection:

$connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB; $conn->open($connStr); //Open the connection to the database

I haven't found a concrete example anywhere so far, so I need to know what the variables $myServer, $myUser etc. need to be in the case of Windows Authentication.

Alternatively, how can I switch to a User-Name and Password Authentication in SQL Server?

LE: Using Microsoft SQL Server 2008 and SQL Server Management Studio


<a href="http://php.net/manual/en/book.pdo.php" rel="nofollow">PDO</a> is the accepted way to connected to different databases in PHP. It also have a <a href="http://www.php.net/manual/en/ref.pdo-sqlsrv.php" rel="nofollow">driver for MS-SQL</a>

Here is an example (<a href="http://www.php.net/manual/en/ref.pdo-sqlsrv.connection.php" rel="nofollow">From PDO MSSQL</a>)

$con = new PDO("sqlsrv:Server=localhost;Database=testdb", "UserName", "Password");


Microsoft made a <a href="http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx" rel="nofollow">driver for PHP</a>, but I think it's better to go with PDO.

You can use PDO_SQLSRV:

$dbh = new PDO("sqlsrv:Server=$hostdb;Database=$dbname", $usr, $psw);

Or use PDO_DBLIB (not available on Windows since PHP 5.3):

$dbh = new PDO("dblib:host=$hostdb;dbname=$dbname", $usr, $psw);


In a related note, the same fate awaits the mysql* extensions if people will ever stop insisting on using them (and the terrible "tutorial" sites stop advocating them) instead of the vastly superior PDO extension. – rdlowrey Feb 12 '12 at 14:58


$dbh = new PDO("sqlsrv:Server=$hostdb;Database=$dbname", $usr, $psw);


According to: <a href="http://www.php.net/manual/en/function.mssql-connect.php" rel="nofollow">http://www.php.net/manual/en/function.mssql-connect.php</a>,

mssql_connect($servername, $username, $password)

is how you connect to a Microsoft SQL database.


  • How to get the name of the logged in user in JSF service in GlassFish
  • How to FindResource (pinvoke) a string resource in C#?
  • Warning: mail(): Failed to Receive in
  • Masterpage CSS link can't resolve ~
  • Using embedded database with Flyway and jOOQ in Maven for continuous integration
  • Whats wrong with this C shell script?
  • Easy convert betwen SQLAlchemy columns and data types?
  • Execute Success but num_rows return 0 [PHP-MySQL]
  • How to export MS Access table into a csv file in Python using e.g. pypyodbc
  • Connect to remote MySQL via Python
  • Displaying Data From Multiple MySQL Tables
  • Cannot send user message with Spring Websocket
  • Why is my SqlPackage DeployReport returning an empty report?
  • firebase, how to update data at a key
  • Attemping to change attributes of $(this) inside success function
  • Converting query results into DataFrame in python
  • How can I get the choice “H2” back in the H2 consol?
  • Getting unused unique values on a SQL table
  • How to get data from **Realm database** using **date object**?
  • Do query loads all the data in memory
  • jQuery Orbit - How to make a Random Slideshow?
  • rspec simple example getting error on request variable in integration test
  • Eloquent paginate function in Slim 3 project using twig
  • SharedPreferences or SQLite Database?
  • Word Open XML Mail Merge
  • Bad request using file_get_contents for PUT request in PHP
  • Recording logins for password protected directories
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • How to extract text from Word files using C#?
  • How to check if every primary key value is being referenced as foreign key in another table
  • Sending data from AppleScript to FileMaker records
  • Can I make an Android app that runs a web view in Chrome 39?
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?