How PHP connects to SQL Server

Source: Internet
Author: User
Tags driver manager sql server driver mssql new set vc9 microsoft website
Description
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) and is not available in SQL Server 2005 and later Mssql_ Connect to the database.
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 Copied to the System32 directory can also be ntwdblib.dll for PHP connection MSSQL2005 or 2008 driver files.
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 the Microsoft website to download SQL Server Driver for PHP is a self-extracting EXE file that you will get so few files after decompression:

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:

<?php  $serverName = "(local)";  $connectionInfo = Array ("UID" = "sa", "PWD" = "admin", "Database" = "db_online");  $conn = Sqlsrv_connect ($serverName, $connectionInfo);  if ($conn) {     echo "Connection established.\n";  } else{     echo "Connection could not being established.\n";     Die (Var_dump (Sqlsrv_errors ()));  }  Sqlsrv_close ($conn);? >

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:

Sqlsrv_connect
sqlsrv_close
sqlsrv_commit
sqlsrv_errors
sqlsrv_fetch
Sqlsrv_fetch_array
Sqlsrv_fetch_metadata
sqlsrv_num_rows
sqlsrv_query
sqlsrv_rollback
sqlsrv_rows_affected
...
Also note that if you use this extension to connect to SQL Server 2005 and the versions of SQL Servers (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:

Array
0 =>array
0 =>string ' IMSSP ' (length=5)
' SQLSTATE ' =>string ' IMSSP ' (length=5)
1 = int-49
' code ' =>int-49
2 =>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.microsoft. com/fwlink/? linkid=163712 ' (length=216)
' 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:http://go.microsoft. com/fwlink/? linkid=163712 ' (length=216)
1 =>array
0 =>string ' IM002 ' (length=5)
' SQLSTATE ' =>string ' IM002 ' ( length=5)
1 =>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)
' 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, download the installation file Sqlncli.msi, after installation.

This is the way PHP connects to SQL Server, and I hope to help you solve similar problems.

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.