Home  >  Article  >  Backend Development  >  How to implement PHP connection to SQL Server2008 on Windows platform

How to implement PHP connection to SQL Server2008 on Windows platform

小云云
小云云Original
2018-02-01 11:09:192615browse

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. Friends in need can refer to the following. , hope it can help everyone.

If you need to communicate with sql server, you need to download The SQL Server Driver for PHP provided by Microsoft at http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx. This is a self-extracting file. After decompression, you will get the following files:

##php_sqlsrv_52_nts_vc6.dllphp_sqlsrv_52_ts_vc6.dll
php_sqlsrv_53_nts_vc6.dll
php_sqlsrv_53_nts_vc9.dll
php_sqls rv_53_ts_vc6 .dll
php_sqlsrv_53_ts_vc9.dll
php_sqlsrv_license.rtf
SQLServerDriverForPHP.chm
SQLServerDriverForPHP_Readme.htm

##PS:The above compressed files can also be downloaded from this site by clicking here. 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 is a help document in the unzipped folder. You can easily find examples. Here the webmaster will introduce 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.

Related recommendations:

C#Example sharing of how to connect to sql server2008 database

PHP 5.6.11 Several ways to access SQL Server2008R2 Detailed explanation of the situation_php example

php56 is used in combination with sql server2008.

The above is the detailed content of How to implement PHP connection to SQL Server2008 on 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