PHP all go to sqlserver Database

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

The mysql_connect or ntwlib. dll mentioned earlier is out of date.


Step 1: Install 2 files

1. Driver provided by Microsoft: Microsoft drivers for PHP for SQL Server

This is 3.0 of: http://www.microsoft.com/en-us/download/details.aspx? Id = 20098

2. Pay attention to the following system requirements:

  • Microsoft SQL Server 2008 R2 native client installed on the same computer on which PHP is running.

Microsoft SQL Server native client should be installed on the computer running PHP. If SQL server runs PHP on a computer, it is useless.


Step 2: copy the corresponding DLL file to the PHP extension folder.

Copy the files decompressed by Microsoft drivers for PHP for SQL Server to the ext directory of PHP Based on the PHP version information, compilation information, and Application Server Information provided by phpinfo. For example, if PHP is 5.3, use the vc6 compiling and Apache server to copy: php_pdo_sqlsrv_53_ts_vc6.dll and php_sqlsrv_53_ts_vc6.dll. For IIS.


Step 3: add the two extensions to the php. ini file.

One of the two extensions is in the common mode and the other is in the PDO mode. If both are correct, you can find the corresponding information in phpinfo.


The sample code is as follows:

<?php/* Specify the server and connection string attributes. */$serverName = "192.168.120.12";$database = "hrdb";/* Get UID and PWD from application-specific files.  */$uid = "username";$pwd = "password"; try {      $conn = new PDO("sqlsrv:server=$serverName;Database = $database", $uid, $pwd);       $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );    }   catch( PDOException $e ) {  echo ($e->message);      die( "Error connecting to SQL Server" );    }   echo "Connected to SQL Server\n";      $query = "select * from TEmployee WHERE FEmployeeID = '000001'";   $stmt = $conn->query( $query );    while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){       var_dump( $row );    }   // Free statement and connection resources.    $stmt = null;    $conn = null; ?>



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.