Php supports connection to the SQL server database and SQL server
1. Software Configuration
Win7 64 + wampserver2.2d-x32 + SQL Server 2008 R2 database. The php version in wamp2.2 is 5.3.10.
The Php environment can also be changed to php + apache.
2. Supports MySQL Server connection Configuration
Php versions earlier than 5.3 have the php_mssql function and can be used. However, php versions 5.3 and later are not supported.
2.1 php connection mssql settings (versions earlier than php5.3)
(1) Open php. ini and set
; Extension = php_mssql.dll; remove the semicolon (;) and restart Apache. If not, perform step 1.
(2) Check whether php_mssql.dll exists in ext under your php installation directory. If not, download another php installation package from www.php.net, which is the most complete package.
If php_mssql.dll already exists in the ext directory, open php. ini and find
Extension_dir = "./ext"
This sentence (or something similar, not necessarily ". /ext ", find" extension_dir "), and then set ". /ext "Modify the complete path of the ext directory of your php installation directory, such as" c:/php/ext "or" c:/program files/php/ext. Restart Apache again. If the problem persists, you may need step 2.
(3) Copy ntwdblib. dll and php_mssql.dll under the php Directory to the system directory of system32, and then restart Apache.
(4) Then you can connect to MSSQL and perform some operations. The connection example is as follows:
$ Conn = mssql_connect ("Instance name or server IP", "username", "password"); // test connection if ($ conn) {echo "connection successful ";} mssql_select_db ("Database Name"); if ($ row = mssql_fetch_array ($ rs) {print_r ($ row );}
2.2 php connection to sqlsrv (php5.3 and later)
Download Microsoft Drivers for PHP for SQL Server, official: http://www.microsoft.com/en-us/download/details.aspx? Id = 20098, I use SQLSRV2.0.
(2) decompress the downloaded file and copy the php_pdo_sqlsrv_53_ts_vc9.dll file and php_sqlsrv_53_ts_vc9.dll file to the ext folder under the php installation directory. The files used by different versions are different.
(3) add in php. ini
Extension = php_sqlsrv_53_ts_vc9.dll
Extension = php_pdo_sqlsrv_53_ts_vc9.dll
There are many; after the extension = ***. dll statement, check whether the position pointed to by extension_dir is correct.
(4) Restart apache and access http: // apache access address /? Phpinfo = 1. If the content in the image below is displayed, the configuration is correct.
(5) write the test code. The test code is as follows:
<? Phpheader ("Content-type: text/html; charset = UTF-8"); $ serverName = "localhost"; // database server address $ uid = "foodcert "; // database username $ pwd = "foodcert"; // Database Password $ connectionInfo = array ("UID" => $ uid, "PWD" => $ pwd, "Database" => "FoodCert"); $ conn = sqlsrv_connect ($ serverName, $ connectionInfo); if ($ conn = false) {echo "connection failed! "; Die (print_r (sqlsrv_errors (), true);} else {echo" connection successful! ";}$ Query = sqlsrv_query ($ conn," select * from database table "); while ($ row = sqlsrv_fetch_array ($ query )) {print_r ($ row) ;}?>
(6) After the above is completed, the connection fails during the test code, because the SQL server nation client is not installed and the local client is used to download the appropriate client, I am using Microsoft SQL Server 2012 Native Client ,:
Http://www.microsoft.com/zh-cn/download/details.aspx? Id = 1, 29065,
(7) after the installation is complete, restart apache and access it to connect successfully.
(8) Note: The ntwdblib. dll file must exist in the folder where the php. ini file is located.
(9) supplement: If you want to know the version of the driver and the version of the local client, you can view. Function description also exists in the document.
3. Reference URL:
(1), http://zhidao.baidu.com/link? Url = required _
(2), http://jingyan.baidu.com/article/cd4c2979ccb866756e6e60c5.html
(3), http://blog.sina.com.cn/s/blog_55d5b4eb0100h3k7.html
(4), http://blog.knowsky.com/204198.htm