In this article I will try to explain how Microsoft SQL Server can integrate with PHP in a Windows environment (WAMP). I will assume that you already have MS SQL server and WAMP installed in your computer.

If you don’t have WAMP installed on your Windows machine then you can get it from “XAMPP” or “WAMP Server 2”.
-http://www.apachefriends.org/en/xampp.html
-http://www.wampserver.com/en/

PHP has discontinued its support for MS SQL. You will have to get the drivers from Microsoft located here:
-http://www.microsoft.com/download/en/details.aspx?id=17308

The .exe file will output several .dll files. Insert these files in the PHP extension directory(….wampbinphpphp5.3.xext). You will have to determine which .dll files suites your WAMP server based on your PHP version and how PHP was compiled. You can read more about it here:
-http://social.technet.microsoft.com/wiki/contents/articles/accessing-sql-server-databases-from-php.aspx

For WAMP Server 2 (PHP verions 5.3.x) we will use “php_sqlsrv_53_ts_vc9.dll” as the SQLSRV driver and “php_pdo_sqlsrv_53_ts_vc9.dll” as the PDO driver. Next you need to edit the “php.ini” file to load the drivers. Add the following lines in the php.ini file:

extension=php_sqlsrv_53_ts_vc9.dll
extension=php_pdo_sqlsrv_53_ts_vc9.dll

There is also an alternate way to load these drivers but, for this article, I will not indulge in the details.

Restart Apache and check your server setting via “phpinfo()” to determine if the drivers/extensions are enabled.If you get an error when Apache restarts such as “unable to load extension…” or “incompatible version….”. This indicates that you do not have the right “sqlsrv” or “pdo” drivers.

Here is a code sample on how you can connect to a MS SQL server using PHP, execute a select query and output the results.

 

/*
Connect to the local server using Windows Authentication and specify
the AdventureWorks database as the database in use. To connect using
SQL Server Authentication, set values for the "UID" and "PWD"
attributes in the $connectionInfo parameter. For example:
$connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database"=>"DB Name");
*/
$serverName = "localhost";
$login = "your MS SQL login";
$password = "your MS SQL Password";
//$connectionInfo = array( "Database"=>"AdventureWorks");
$connectionInfo = array("UID" => $login, "PWD" => $password, "Database"=>"DEV");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn )
{
echo "Connection established.
";

if( $conn === false )
{
echo "Could not connect.
";
die( print_r( sqlsrv_errors(), true));
}

$tsql = "select * from dbo.testTable";

/* Execute the query. */
if( ($stmt = sqlsrv_query( $conn, $tsql)))
{
echo "Statement executed.
";

/* Retrieve each row as an associative array and display the results.*/
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{
echo $row['id'].", ".$row['Name']."
";
}

/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);

}
else
{
echo "Error in statement execution.
";
die( print_r( sqlsrv_errors(), true));
}

}
else
{
echo "Connection could not be established.n";
die( print_r( sqlsrv_errors(), true));
}

/* Close the connection. */
sqlsrv_close( $conn);