PHP is a natural support for MySQL, but sometimes you want it to access SQL Server, what should I do?
Recently found a bit of information, the test success of PHP access to sqlsvr several cases, limited to the time, has not tested more different environments, the test records are as follows:
--------------------------------------
Test environment: Win7 x64 sp1,iis 7.5, Apache 2.4 32-bit version, PHP 5.2.6 Win32, PHP 5.6.11 Win32 TS (thread-safe edition)
Note: Because the "Microsoft Drivers for PHP for SQL Server" driver currently has only 32-bit editions, it is not recommended to use a PHP 64-bit if you are a friend of a SQL Server database, otherwise you will not be able to connect to the SQL Server database Problem!
--------------------------------------
PHP 5.2.6 successfully accessed sql2000 on IIS
PHP 5.2.6 successfully accessed sql2008r2 on IIS
PHP 5.6.11 successfully visited SQL2008R2 on Apache
------------------------------------------
"5.2.6-SQL2000"
Why use 5.2.6 so old PHP version to do test, because PHP 5.2 version, built-in php_mssql.dll module,
The extension=php_mssql.dll configuration option in PHP.ini is turned on, and the default seems to support mssql2000.
Open Phpinfo to see the relevant support module content:
Ps. IIS 7.0 installation of PHP is now convenient, can be installed through a third-party gadget PHP Manager, you can easily switch the PHP version under IIS, this is not the main content of this article, here is not introduced;
PHP Manager for IIS 7 Download installation address: http://phpmanager.codeplex.com/
Write a small routine for the test:
1<?PHP2 $server= "127.0.0.1\sql2000";//Server IP Address, if local, can be written as localhost3 $uid= "SA";//User name4 $pwd= "Yoooko";//Password5 $database= "Master";//Database name6 7 //Database connection8 $conn=mssql_connect ($server,$uid,$pwd) or die("Connect Failed");9mssql_select_db ($database,$conn);Ten One //Execute Query Statement A $query= "SELECT * FROM CS"; - $row=mssql_query ($query); - the //Print output Query results - while($list=mssql_fetch_array ($row)) - { - Print_r($list); + Echo"<br>"; - } +?>
Running the results under IIS 7.5 is as follows: Success!
"5.6.11-SQL2008R2"
More than 5.6 of PHP has no php_mssql.dll, seemingly no longer natively support the sql2000 version of SQL Server, to pass the third-party module, on the Microsoft Official website found:
Microsoft Drivers for PHP for SQL Server
http://www.microsoft.com/en-us/download/details.aspx?id=20098
The download link address has four files:
- SQLSRV30. Exe
- SQLSRV31. Exe
- SQLSRV32. Exe
- SQLSRV40. Exe
Supports different PHP versions, respectively
- Version 4.0 supports PHP 7.0+
- Version 3.2 supports PHP 5.6, 5.5, and 5.4
- Version 3.1 supports PHP 5.5 and 5.4
- Version 3.0 supports PHP 5.4.
If it is php 5.2 to download a SQLSRV20.EXE, this file is now easy to find on the Internet;
(Although 5.2 of this old version is long overdue, it is necessary to take into account certain circumstances)
Php_pdo_sqlsrv_52_ts_vc6.dll
Php_sqlsrv_52_ts_vc6.dll
1. I have PHP 5.6.11 Win32 TS in hand.
Php_sqlsrv_56_ts.dll
Php_pdo_sqlsrv_56_ts.dll
These two files are copied to the Php\ext directory,
2. Add the following two sentences to the php.ini:
Extension=c:\php5\ext\php_sqlsrv_56_ts.dll
Extension=c:\php5\ext\php_pdo_sqlsrv_56_ts.dll
3. Restart IIS or Apache,
4. Through the phpinfo can see the following module display, our PHP should be able to access the SQL Server 2008r2 at this time;
5, write a small program to verify:
1<?PHP2 Try { 3 $dbName= "Sqlsrv:server=127.0.0.1\sql2008r2;database=master"; 4 $dbUser= "SA"; 5 $dbPassword= "Yoooko"; 6 $db=NewPDO ($dbName,$dbUser,$dbPassword); 7 if($db) 8 { 9 Echo"Database Connect succeed.<br/>"; Ten } One } A - Catch(pdoexception$e) - { the $content=Iconv("UTF-8", "GBK",$e-getMessage ()); - Echo $content. "<br/>"; - } - + //echo "Hello PDO to MS sqlsrv!"; - +?>
If the following prompt appears:
You may also need to install MICROSOFT®ODBC Driver for SQL server®-Windows
: https://www.microsoft.com/zh-cn/download/details.aspx?id=36434
If it's correct, it should be OK:
PS. Because the new module accesses SQL Server 2008R2 via PDO, if it is native access, it is accessible directly via the IP address and sa account.
If PHP and sql2008r2 on different computers, but also to set the TCP port 1433来 access, see the second picture below, the dynamic port to 0, the port is set to 14333 if your SQL2000 has occupied 1433, access address after adding a, xxxx port number
SQLSRV:SERVER=192.168.1.XXX\SQL2008R2,14333;D atabase=master
PHP 5.6.11 access to SQL SERVER2008R2