Php supports connection to the SQL server database and SQL server

Source: Internet
Author: User
Tags microsoft drivers for php for sql server

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, 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: 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:// 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), Url = required _






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.