Home > Article > Backend Development > PHP5.3.X connects to MSSQL database_PHP tutorial
Under Windows systems, PHP versions above 5.3 no longer support the mssql extension, so if you need to communicate with the sql server, you need to download it yourself from http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx The SQL Server Driver for PHP provided by Microsoft. This is a self-extracting file. After decompression, you will get the following files:
php_sqlsrv_52_nts_vc6.dll
php_sqlsrv_52_ts_vc6.dll
php_sqlsrv_53_nts_vc6.dll
php_sqlsrv_53_nts_vc9.dll
php_sqlsrv_53_ts_vc6.dll
php_sqlsrv_53_ts_vc9.dll
php_sqlsrv_license.rtf
SQLServerDriverForPHP.chm
SQLServerDriverForPHP_Readme.htm
Among them, 52 and 53 represent the versions of PHP 5.2.
According to your configuration, copy the corresponding DLL file to the ext folder of the PHP installation directory. Next, open php.ini and add the following statements to open the php_sqlsrv and php_pdo_sqlsrv extensions:
——————————————————–
[PHP_PDO_SQLSRV]
extension=php_pdo_sqlsrv_53_ts_vc6.dll
[PHP_SQLSRV]
extension=php_sqlsrv_53_ts_vc6.dll
————————————————
The 53 here means php5.3. If yours is version 5.2, change it to 52. If your PHP version is thread-safe, then there should be a php5ts.dll in your PHP installation directory, which is the same as The two lines of statements here correspond. If it is php5nts.dll, then the above statement should be:
—————————————————————-
[PHP_PDO_SQLSRV]
extension=php_pdo_sqlsrv_53_nts_vc6.dll
[PHP_SQLSRV]
extension=php_sqlsrv_53_nts_vc6.dll
—————————————————-
There are dll files for each version in the compressed package, you can check it carefully.
After turning on the extension, restart apache so that you can connect to sqlserver. However, there is one more thing to note. If you have not installed Microsoft SQL Server 2008 R2 Native Client, you must go to http://msdn.microsoft.com/en- Download and install us/library/cc296170(SQL.90).aspx, because this extension package from Microsoft requires this support.
After everything is in place, you can write PHP code. If you downloaded The SQL Server Driver for PHP, there is a help document in the unzipped folder. You can easily find the example here. Let’s introduce a simple example:
//Service name for local testing
$serverName = “(127.0.0.1)”;
//Use sql server authentication, the parameters are in the form of an array, one is the user name, password, database name
//If you are using windows authentication, you can remove the username and password
$connectionInfo = array( “UID”=>”root”,
“PWD”=>”root2010″,
"Database"=>"master");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn )
{
echo “Connection established.n”;
}
else
{
echo “Connection could not be established.n”;
die( print_r( sqlsrv_errors(), true));
}
?>
If the connection is unsuccessful, restart the sql server and try again.
Excerpted from Chris Mao’s column