PHP supports connection to SQL Server database

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

PHP supports connection to SQL Server database

1. Software configuration

Win7 +wampserver2.2d-x32+sql Server R2 database, the PHP version in wamp2.2 is 5.3.10.

PHP environment can also be changed to Php+apache.

2. Support connection to MySQL server configuration

Prior to PHP version 5.3, there are php_mssql features that can be used, but 5.3 and later versions are not supported.

2.1, PHP connection MSSQL Setup (php5.3 previous version)

(1), open php.ini, will
Extension=php_mssql.dll the semicolon (;) in front of it, and then restart Apache. If not, take the 2nd step.

(2) Check your PHP installation directory under ext There is no php_mssql.dll exist, if not, download a PHP installation from www.php.net, to download the compression package is the most complete.
If the Ext directory already has the Php_mssql.dll, then you need to open php.ini, find
Extension_dir = "./ext"
This sentence (or similar, not necessarily "./ext", Look for "Extension_dir"), then change "./ext" to the full path of the ext directory of your PHP installation directory, such as "C:/php/ext", or "C:/Program files /php/ext "So. Then restart Apache again. If it doesn't work, it may take the 3rd step.

(3) Copy the Ntwdblib.dll and Php_mssql.dll in the PHP directory to the system32 system directory, and then re-start Apache.

(4) Then you can connect the MSSQL and do some operation. The connection examples are as follows:

2.2, PHP connection sqlsrv (php5.3 and above version)

(1), download Microsoft Drivers for PHP for SQL Server, official: http://www.microsoft.com/en-us/download/details.aspx?id= 20098, I'm using the SQLSRV2.0.

(2), unzip the downloaded file to copy the Php_pdo_sqlsrv_53_ts_vc9.dll file and the Php_sqlsrv_53_ts_vc9.dll file to the Ext folder in the PHP installation directory. There are different files used here depending on the version.

(3), add in php.ini

Extension=php_sqlsrv_53_ts_vc9.dll

Extension=php_pdo_sqlsrv_53_ts_vc9.dll

to a lot; After the Extension=***.dll statement, note that Extension_dir points to the correct location.

(4), restart Apache, and then access the Http://apache access address/? Phpinfo=1, the content in the image below appears, and 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 user Name $pwd = "Foodcert"; Database Password $connectioninfo = array ("UID" = = $uid, "PWD" = = $pwd, "database" = "Foodcert"); $conn = Sqlsrv_connect ($ ServerName, $connectionInfo); if ($conn = = False) {echo ' Connection failed! ";d ie (Print_r (sqlsrv_errors (), True));} Else{echo "Connection is successful! ";  } $query = sqlsrv_query ($conn, "SELECT * from database table"), while ($row = Sqlsrv_fetch_array ($query)) {  print_r ($row);}? >

(6), after the completion of the test code will appear when the connection failed, because the SQL Server Nation client, the local client, to download the appropriate client, I am using Microsoft? SQL Server? Native Client,:

http://www.microsoft.com/zh-cn/download/details.aspx?id=29065,

(7), after the installation is complete, after restarting Apache, then access will be able to connect successfully.

(8), note: You must have a Ntwdblib.dll file in the folder where the php.ini file exists.

(9), add: If you want to know the version of the driver and the local client version, you can view the http://php.net/manual/en/ sqlsrv.requirements.php, in the content, the content is the PHP official website document content, above describes the detailed steps, must learn to go to the official website to read the English document, is very detailed. The function description also exists in the document.

PHP supports connection to SQL Server database

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: info-contact@alibabacloud.com 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.