How PHP connects SQL server_php tips

Source: Internet
Author: User
Tags microsoft sql server sql server driver mssql new set phpinfo vc9 what web server microsoft website

Description
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) and is not available in SQL Server 2005 and above Mssql_ Connect to the database.
2:php5.3.x no longer support Php_mssql.dll expansion Library, the timely use of php5.2.x Php_mssql.dll also can not be used. Microsoft has a SQL Server extension for PHP (Windows version), which is useful for developing SQL Server applications using PHP in Windows, which facilitates the use of SQL Server to develop PHP platform connection SQL Server database management system.
one, the following version of php5.3 to connect SQL Server
5.3 of the following version of the expansion with a php_mssql.dll; the extension of the database, you can use this extended link database, limited to the link to the lower version of the database.
Specific toStepsAs follows:
1: First install SQL Server, Super Bad installation, I was installed before the 2008 version of Windows expired after the use of, and then reinstall is not installed, the last reload system installed on the
2: Determine when the SQL installed with a mixed authentication mode, or SQL authentication mode, and then open the PHP configuration file (php.ini), open MSSQL extension (extension=php_pdo_ Mssql.dll before the semicolon is removed) and needs to be mssql.secure_connection = on restart after the entry into force.
If it's going well, you can already connect to the database, and if you don't, you'll need to continue the low configuration:
3: Check the version of the Ntwdblib file (php/below and below apache/) to download the correct version of Ntwdblib.dll (2000.80.194.0) overwrite the existing DLL file (Ntwdblib.dll,php_mssql.dll Copying to the System32 directory can also be ntwdblib.dll for driver files for PHP connection MSSQL2005 or 2008.
4: Test Connection: Mssql_connect (' localhost,1433 ', ' username ', ' password ');
second, php5.3+ connect SQL Server
In fact, less than 5.3 of PHP version has been used, and security and compatibility is not good, so the high version of PHP is still more common. Practice shows that the lower version of PHP Connection database success rate is low (more than 2005 version is almost impossible to use), the recommended use of php5.3+ PHP to use Microsoft's dedicated extended 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, decompression after you will get so many files:

52, 53 is PHP 5.2.x and 5.3.x version, choose to match your PHP version; VC6 or vc9 choice depends on what Web server software you use, if you are using IIS then choose Vc9, if it is Apache choose VC6, TS and NTS choices to see if the version of PHP you have installed is either thread-safe or non-threaded, TS is thread-safe and NTS is thread-safe.
If you don't know you can see the Zend Extension build in Phpinfo the following figure:

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

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 to be 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_, 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 SQL Server 2005 and the above version of SQL Server (such as SQL Server 2008), you will also need to install SQL Server Native Client on the machine first
Otherwise, you will receive the following error:

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.micros oft.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 found and no default driver specified ' (length=71)
' Message ' =>string ' [MICROSOFT][ODBC Driver Manager] did not find the data source name and did not specify a default driver ' (length=71)
. . . .

Workaround: You need to install SQL Server 2008 Native Client ODBC Driver, download the installation file Sqlncli.msi, and then install it.

The above is the way PHP connects SQL Server, I hope to solve similar problems to help.

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.