PHP Connection MSSQL sql2008

Source: Internet
Author: User
Tags file copy mssql php server php database stmt

Summary

SQL Server

1. Download the DLL provided by Microsoft

: http://www.microsoft.com/en-us/download/details.aspx?id=20098

php5.4 need to download SQLSRV30. EXE, this exe is a self-extracting file, downloaded after double-click the selected path decompression will get a bunch of DLLs and documentation.

2, the corresponding PHP version of the DLL file copy to the PHP ext directory

TS refers to thread safety (ThreadSafe), NTS refers to the thread is unsafe, depending on the version of PHP installed to choose, if not sure, try it separately;

I use the php5.4, will php_sqlsrv_ _ts.dllCopy to the ext directory,

Example: D:\Program files\php 5.4\ext

3. Modify the php.ini file

Add two lines: Extension=php_pdo_sqlsrv_54_ts.dll

Extension=php_sqlsrv_54_ts.dll

Modify one line: Mssql.secure_connection = Off to On

4. Restart Apache

5. Configuring MS SQL Server2005

A. Open SQL Server Configuration Manager: SQL Server Config Manager, open the Protocol;

B, enable "Named Pipes" and "TCP/IP", the default is disabled;

C, right click on "TCP/IP", select "Properties", select "IP address", "Allip" under "TCP dynamic port" after filling 1433;

D. Restart SQL Server.

Note: Remember to turn on SA login.

6. Install Microsoft SQL Server Native Client.msi

Install Microsoft SQL Server Native Client.msi, sub-X64 and X86 according to your system on the machine where the PHP server is located.

: http://www.microsoft.com/zh-cn/download/details.aspx?id=29065

This tool does not support XP system Oh, so XP to use PHP5.3 Bar ~

In addition, this is installed in the PHP environment under the server PS: not a standalone database server, please automatically ignore.

7. PHP Database Code

<?PHP/*to connect to the database, the 5.4 function is no longer mssql_connect:*/$conInfo=Array("Database" = "att2008", "UID" = "att2008", "PWD" = "att2008");$conn=sqlsrv_connect ("127.0.0.1",$conInfo); /*Determine whether the connection is successful or not:*/if($conn==false ){ die(Print_r(Sqlsrv_errors (),true));}Else{Echo("Yes<br>");} $table _name= "USERINFO";/*If your fork's database table name is Chinese, remember to add this sentence:*///$table _name = Iconv ("UTF-8", "GB2312", "the Chinese table name of the Ox Fork");/*Query statement:*/$rs=sqlsrv_query ($conn, "SELECT * from".$table _name); if($rs==false){Echo("False<br>");}Else{ while($row= Sqlsrv_fetch_array ($rs)){Print_r($row);} SQLSRV_FREE_STMT ($rs); Sqlsrv_close ($conn);}?>

8, at this point, the online tutorial copy completed, but this method does not support XP system! (I use Phpstudy, very convenient one-piece multi-integrated service)

However, mssql.php is turned on in this method, so the following code can be used to connect sql2008 in XP

<?PHP$server= "lpp-20140312390\s2008";//Server IP Address, if local, can be written as localhost, this example is multiple instances$uid= "SA";//User name$pwd= "sql2008";//Password$database= "Test";//database name//database connection$conn=mssql_connect ($server,$uid,$pwd) or die("Connect Failed"); mssql_select_db ($database,$conn);//Execute Query Statement$query= "SELECT * FROM Demo";$row=mssql_query ($query); //Print output Query results while($list=mssql_fetch_array ($row)){       Print_r($list); Echo"<br>";}//PDO Connection is unsuccessful, there is no Microsoft Library, 2012 library does not support xp//$conn = new PDO ("sqlsrv:server= (lpp-20140312390\s2008);D atabase=test", "sa", " Sql2008 "); $conn->setattribute (Pdo::attr_errmode, pdo::errmode_exception);//$query = ' SELECT * from demo '; $stmt = $conn->query ($query); while ($row = $stmt->fetch (PDO::FETCH_ASSOC)) {//Print_r ($row);//}

Connection SqlServer2008 in 9,XP system

9.1 Same Download http://www.microsoft.com/en-us/download/details.aspx?id=20098

Find Sqlsrv20, which is the Support XP DLL, the support range includes 5.3, 5.2, does not support 5.4, where NT is thread-safe, v6v9 meaning is the Apache is written in which language, is c++9 or c++6, this installation when the prompt error will change a

Download Microsoft SQL Server Native Client.msi, this is a must

9.2 Restarting the server

Test code

<?PHP//PDO Connection Mode//$conn = new PDO ("SQLSRV:SERVER=LPP-20140312390\S2008;DATABASE=CDTXYYBJP", "sa", "sql2008");Try {   $conn=NewPDO ("SQLSRV:SERVER=LPP-20140312390\S2008;DATABASE=CDTXYYBJP",NULL,NULL); $conn->setattribute (Pdo::attr_errmode, PDO::errmode_exception);}Catch(pdoexception$e ) {    die("Error Connecting to SQL Server" ); }Echo"Connected to SQL server\n";$query= ' Select * from sysobjects '; $stmt=$conn->query ($query );  while($row=$stmt->fetch (PDO::Fetch_assoc)) {p ($row ); }///* Specify the server and connection string attributes. *///$serverName = "lpp-20140312390\s2008";//$connectionInfo = Array ("Database" = "CDTXYYBJP");//* Connect using Windows authentication. *///$conn = Sqlsrv_connect ($serverName, $connectionInfo);/if ($conn = = = False)//{//echo "Unable to connect.<  ;/br> ";//Die (Print_r (Sqlsrv_errors (), true));//}//Query SQL Server for the login of the user accessing the// Database. $tsql = "Select CONVERT (varchar (+), SUSER_SNAME ())",//$stmt = sqlsrv_query ($conn, $tsql),//if ($stmt = = = False)// {//echo ' Error in executing query.</br> ';//Die (Print_r (Sqlsrv_errors (), true);//}////Retrieve and Display the results of the query. *///$row = Sqlsrv_fetch_array ($stmt);//echo "User login:". $row [0]. " </br>///* FREE statement and connection resources. *///sqlsrv_free_stmt ($stmt);//Sqlsrv_close ($conn);

PHP connection MSSQL sql2008

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.