Home  >  Article  >  Backend Development  >  How to connect php to SQL Server2008 under Windows platform?

How to connect php to SQL Server2008 under Windows platform?

黄舟
黄舟Original
2017-07-26 14:47:021150browse

This article mainly introduces the method of connecting PHP to SQL Server 2008 on the Windows platform. It analyzes the relevant dll dynamic link library files and corresponding configuration and usage methods required for PHP to connect to SQL Server 2008 on the Windows platform in the form of examples. Friends in need You can refer to the following example

This article describes how to implement PHP connection to SQL Server2008 on the Windows platform. Share it with everyone for your reference, the details are as follows:

If you need to communicate with sql server, you need to download The SQL Server Driver for Microsoft provided by msdn.microsoft.com/en-us/sqlserver/ff657782.aspx PHP. 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


##PS:You can also click here to download the above compressed file.

Among them, 52 and 53 represent the versions of PHP 5.2. Server.

According to your configuration, copy the corresponding DLL file to the ext folder of the PHP installation directory, then 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 represents 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 corresponds to the two lines of statements here. 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 versions for each version in the compressed package dll file, you can check it carefully.

After turning on the extension, restart apache so that you can connect to sqlserver, but 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 Download and install /en-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 will be a help document in the unzipped folder. You can easily find examples. Here the webmaster introduces a simple example:


<?php
//本地测试的服务名
$serverName = "(127.0.0.1)";
//使用sql server身份验证,参数使用数组的形式,一次是用户名,密码,数据库名
//如果你使用的是windows身份验证,那么可以去掉用户名和密码
$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.

The above is the detailed content of How to connect php to SQL Server2008 under Windows platform?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn