How to connect php to sqlserver

Source: Internet
Author: User
Tags driver manager sql server driver microsoft website
This article mainly introduces how to connect php to sqlserver, the problems encountered during the connection process, and the solutions. if you need them, refer to the instructions below:
1: PHP5.2.x has an extension of php_mssql.dll to connect to SQL server. However, this dll is only used to connect to earlier SQL server versions (versions earlier than 2000 ), mssql_connect cannot be used to connect to the database in SQL server 2005 or later versions.
2: php5.3.x no longer supports the php_mssql.dll extension library, and php_mssql.dll in php5.2.x cannot be used in a timely manner. Microsoft has developed an SQL Server extension for PHP (for Windows). For SQL Server applications developed using php in Windows, this extension facilitates the use of SQL Server to develop the php platform to connect to the SQL server database management system.
I. connect to SQL server in versions earlier than php5.3
In versions earlier than 5.3, php_mssql.dll is included in the extension. you can use this extension to connect to the database (only to link to lower-version databases ).
The procedure is as follows:
1: First install SQL server. it is not easy to install. I installed version 2008 before. Windows will not be available after it expires, and then the system will not be installed after the reinstallation.
2: Make sure that the mixed authentication mode or SQL authentication mode is used when installing SQL, and then open the php configuration file (php. ini), enable the mssql extension (remove the semicolon before extension = php_pdo_mssql.dll), and remove mssql. secure_connection = On takes effect after restart.
If the connection is successful, you can connect to the database. if the connection fails, you need to continue with the following 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) overwrite the existing DLL file (put ntwdblib. dll and php_mssql.dll can also be copied to the system32 directory) ntwdblib. dll is used for PHP to connect to the driver file of MSSQL2005 or 2008.
4: Test connection: mssql_connect ('localhost, 123456', 'username', 'password ');
2. connect php5.3 + to SQL server
Php versions earlier than version 5.3 are rarely used, and the security and compatibility are poor. Therefore, php versions later are common. Practice has proved that the success rate of connecting to the database with a lower version of php is relatively low (Version 2005 or above is almost unavailable). We recommend that you use php5.3 + php to connect to the sqlserver database with Microsoft's dedicated extension SQLSRV.
The procedure is as follows:
1: download SQL Server Driver for PHP on the Microsoft website. it is a self-extracting EXE file. after decompression, you will get the following files:

4: connection test:

 "sa","PWD"=>"admin","Database"=>"db_online");  $conn = sqlsrv_connect( $serverName, $connectionInfo);  if( $conn ){     echo "Connection established.\n";  }else{     echo "Connection could not be established.\n";     die( var_dump(sqlsrv_errors()));  }  sqlsrv_close( $conn);?>

Note that the connection here uses sqlsrv_connect instead of mssql_connect. in this version, there are several functions:
This extension adds a series of functions starting with sqlsrv _ to php, 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
...
In addition, if you use this extension to connect to SQL server 2005 and later versions of SQL server (such as SQL server 2008), you also need to install SQL Server Native Client on the machine first.
Otherwise, the following error occurs:

Array
0 => array
0 => string 'imss' (length = 5)
'Sqlstate' => string 'imssp '(length = 5)
Int-49
'Code' => int-49
2 => string 'This extension requires the Microsoft SQL Server 2012 Native Client. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink? LinkId = 163712 '(length = 216)
'Message' => string 'This extension requires the Microsoft SQL Server 2008 Native Client. Access the following URL to download the Microsoft SQL Server 2008 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] no data source name is found and no default driver is specified' (length = 71)
'Message' => string' [Microsoft] [ODBC driver manager] no data source name found and default driver not specified '(length = 71)
....

Solution: install the SQL Server 2008 Native Client ODBC Driver, download the installation file sqlncli. msi, and then install it.

The above is the method for connecting php to SQL server. I hope it will help you solve similar problems.

For more articles about how to connect php to SQL server, refer to PHP Chinese network!

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.