This article will show you how to connect to Microsoft’s MSSQL database via PHP. Typically, most PHP configurations involves the use of MySQL database, but every once in a while, there might be a need to connect PHP with MSSQL. So, here’s a sample script to get you started.

$sql_server = "localhost";
$sql_user   = "username";
$sql_pass   = "password";
$sql_db     = "database"; 
// Connect to the database
$db = mssql_connect($sql_server, $sql_user, $sql_pass)
or die("Can't connect to the MSSQL Server."); 
// Select a database
$select = mssql_select_db($sql_db, $db)
or die("Can't open the database " .$sql_db); 
// SQL statement
$query = "SELECT * FROM tablename WHERE id='3'";
// Execute the SQL query
$result = mssql_query($query);
// Display rows returned
$num_rows = mssql_num_rows($result); 
echo $num_rows." rows"; 
// Display results 
while($row = mssql_fetch_array($result)) {
  echo $row["id"];
  echo $row["first"];
// Close DB connection