PHP connection to SQL Server

Source: Internet
Author: User
Tags driver manager microsoft sql server sql server driver mssql new set phpinfo vc9

There's a PHP project that connects to SQL Server for two days, and by the way, learn SQL Server
The 1:php5.2.x itself has a php_mssql.dll extension to connect to SQL Server, but this DLL is only used to connect to the lower version of SQL Server (version 2000 below), in SQL Server Versions 2005 and later cannot connect to the database using Mssql_connect.
2:php5.3.x no longer support the Php_mssql.dll expansion library, the timely use of php5.2.x in the Php_mssql.dll also can not be used. Microsoft has a SQL Server extension specifically for PHP (version of Windows), which is useful for developing SQL Server applications under Windows with SQL Server to develop a PHP platform to connect SQL Server database management system.
One, php5.3 the following version to connect to SQL Server
5.3 The following version of the extension comes with a php_mssql.dll; the extension of the database can be used to make use of this extended link database (limited to linking the lower version database).
the specific steps are as follows:
1: First install SQL Server, Super Bad installation, I installed the 2008 version, Windows expires after the use of, and then reinstall can not be installed, the last reload system installed
2: Make sure that SQL is installed using mixed authentication mode, or SQL authentication mode, and then open the PHP configuration file (php.ini), turn on the MSSQL extension (extension=php_pdo_ Mssql.dll in front of the semicolon) and need to take mssql.secure_connection = on reboot to take effect.
if it is more successful, you can already connect to the database, if you do not need to continue the low configuration:
3: Check the version of the Ntwdblib file (php/below and apache/below) to download the correct version of Ntwdblib.dll (2000.80.194.0) to overwrite the existing DLL file (ntwdblib.dll,php_ Mssql.dll can also be copied to the System32 directory Ntwdblib.dll the driver files used for PHP connection MSSQL2005 or 2008.
4: Test Connection: Mssql_connect (' localhost,1433 ', ' username ', ' password ');
ii. php5.3+ connecting SQL Server
In fact, the PHP version under 5.3 is rarely used, and the security and compatibility are not good, so the high version of PHP is still more common. The practice proves that the lower version of PHP Connection database success rate is relatively low (more than 2005 of the version is almost impossible to use), we recommend using php5.3+ PHP to use Microsoft's dedicated extension SQLSRV to connect SQL Server database
The steps are as follows:
1: First to Microsoft Web site download SQL Server Driver for PHP ( is a self-extracting EXE file, After decompression you will get so many files:

52, 53 is the PHP version of 5.2.x and 5.3.x, choose to match your PHP version, VC6 or vc9 choice depends on what you use the Web server software, if you are using IIS then choose Vc9, if it is Apache chose VC6, The choice of TS and NTS depends on whether the version of PHP you are installing is thread-safe or non-threaded, and TS is thread safe and NTS is non-thread safe.
If you do not know can see in phpinfo Zend Extension build this property such as:

2: Copy the extension to the Php/ext directory, in the php.ini file, add the code:
extension= PDO Extension under EXT (for PDO)
extension= Extension under Ext
3: Restart the server, open phpinfo (); see the following status to prove that the add extension is successful,

4: Connection test:

1<?PHP2     $serverName= "(local)";3     $connectionInfo=Array("UID" = "sa", "PWD" and "admin", "Database" = "Db_online");4     $conn= Sqlsrv_connect ($serverName,$connectionInfo);5     if($conn ){6          Echo"Connection established.\n";7}Else{8          Echo"Connection could not being established.\n";9           die(Var_dump(Sqlsrv_errors ()));Ten     } OneSqlsrv_close ($conn); A?>

Note that the connection here is not with Mssql_connect but with Sqlsrv_connect, and in this version there are several functions:
This extension provides PHP with a new set of functions that begin with sqlsrv_, which are commonly used as follows:

1 Sqlsrv_connect2 sqlsrv_close3 sqlsrv_commit4 sqlsrv_errors5 Sqlsrv_fetch6 Sqlsrv_fetch_array7 Sqlsrv_fetch_metadata8 sqlsrv_num_rows9 sqlsrv_queryTen sqlsrv_rollback One sqlsrv_rows_affected A. . .

For more information, refer to: Development documentation

It is also important to note that if you use this extension to connect SQL Server 2005 and the above versions of SQL Server (such as SQL Server 2008), you will need to install the SQL Server Native Client on the machine first
Otherwise, the following error will occur:

1 Array20 =Array30 =string' Imssp ' (length=5)4' SQLSTATE ' =string' Imssp ' (length=5)51 =>int-496' Code ' =>int-4972 =string' This extension requires the Microsoft SQL Server Native Client. Access the following URL to download the Microsoft SQL Server-Native Client ODBC driver for X86:http://go.micros linkid=163712 ' (length=216)8' Message ' =string' This extension requires the Microsoft SQL Server Native Client. Access the following URL to download the Microsoft SQL Server, Native Client ODBC driver for X86: com/fwlink/? linkid=163712 ' (length=216)91 =ArrayTen0 =string' IM002 ' (length=5) One' SQLSTATE ' =string' IM002 ' (length=5) A1 =int0 -' Code ' =int0 -2 =string' [MICROSOFT][ODBC Driver Manager] did not discover the data source name and did not specify a default driver ' (length=71) the' Message ' =string' [MICROSOFT][ODBC Driver Manager] did not discover the data source name and did not specify a default driver ' (length=71) -. . . .

WORKAROUND: You need to install SQL Server Native Client ODBC Driver to download the installation file sqlncli.msi: The 0e67502a-22b4-4c47-92d3-0d223f117190/sqlncli.msi is ready after installation.

Summary: To complete this, I put on my computer for most of the day, in the middle of a variety of errors, the overall feeling of Microsoft's things too bad. It is not the last resort to use PHP to link to SQL Server, link MySQL does not have to.

PHP connection to SQL Server

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.