PHP Connection SQLServer2008

Source: Internet
Author: User
Tags error handling sql server driver vc9

Original: PHP connection SQLServer2008

PHP's own MSSQL extension php_mssql.dll is for SQL Server 2000 and is not connected to 2008?! -_-!! To use SQL Server 2005 or later, use Microsoft's latest third-party extended SQL Server driverfor PHP for PHP only, and the latest version is 2.0:

http://www.microsoft.com/downloads/details.aspx?familyid=DF4D9CC9-459C-4D75-A503-AE3FCEB85860&displaylang= En

Download the driver, download and install the release program, which has the following files:

Php_pdo_sqlsrv_52_nts.dll

Php_pdo_sqlsrv_52_ts.dll

Php_pdo_sqlsrv_53_nts_vc6.dll

Php_pdo_sqlsrv_53_nts_vc9.dll

Php_pdo_sqlsrv_53_ts_vc6.dll

Php_pdo_sqlsrv_53_ts_vc9.dll

Php_sqlsrv_52_nts.dll

Php_sqlsrv_52_ts.dll

Php_sqlsrv_53_nts_vc6.dll

Php_sqlsrv_53_nts_vc9.dll

Php_sqlsrv_53_ts_vc6.dll

Php_sqlsrv_53_ts_vc9.dll

Sqlserverdriverforphp.chm (manual, English good enough, can see, hehe)

Sqlserverdriverforphp_license.rtf

Sqlserverdriverforphp_readme.htm (Readme file)

About the difference between VC6 and VC9

the VC6 version is compiled with the Visual Studio 6 compiler, and if you are using apache+php under Windows, select the VC6 version.

the VC9 version is compiled with the Visual Studio 2008 compiler, and if you are using iis+php under Windows , select the VC9 version.

Start configuration

Select Php_sqlsrv_53_ts.dll and Php_pdo_sqlsrv_53_ts.dll. Copy the file to the Ext directory under the PHP folder, and then in the config file php.ini
Extensions after the following add:

Extension=php_sqlsrv_53_ts.dll

Extension=php_pdo_sqlsrv_53_ts.dll

Restart the Apache service again.

Finally test whether the success, in PHP to perform phpinfo () view sqlsrv, look at the picture Red line, so that the connection is successful!!


1.Windows Authentication Connection

<?php

$serverName = "(localhost)";

$connectionInfo =array ("Database" = "test", "connectionpooling" =>false);

$conn = Sqlsrv_connect ($serverName, $connectionInfo);

if ($conn = = False)

{

echo "Connection failed! ";

die (Print_r (Sqlsrv_errors (), true) );

}

?>

2.SQL Server Authentication Connection

<?php

$serverName = "localhost"; Database Server address

$uid = "sa"; Database user Name

$pwd = "123"; Database Password

$connectionInfo = Array ("UID" = = $uid, "PWD" + = $pwd, "Database" = "cart");

$conn = Sqlsrv_connect ($serverName, $connectionInfo);

if ($conn = = False)

{

echo "Connection failed! ";

die (Print_r (Sqlsrv_errors (), true) );

}

Execute SQL statement with result set

$query = sqlsrv_query ($conn, "select * from Tb_goods");

while ($row = Sqlsrv_fetch_array ($query))

{

Echo$row[0]. " -----". $row [1]." <br/> ";

}

Execute an SQL statement that changes or deletions

$rs =sqlsrv_query ($conn, "Update tb_goods setname= ' China gin ' where name= ' Jin Wine '");

$num =sqlsrv_rows_affected ($RS);//Returns the number of modified rows

if ($num >0)
{
Echo ' modified successfully! '. $num ;

}
Else
{
echo ' modification failed! ‘;
}

?>

The API name for SQL Server driverfor PHP is sqlsrv. All sqlsrv functions begin with sqlsrv_ ,

SQL Server Driver forphp contains the following functions:

Function description
Sqlsrv_begin_transaction start the transaction.
Sqlsrv_cancel cancels the statement and discards all pending results for the corresponding statement.
Sqlsrv_client_info provides information about the client.
Sqlsrv_close Close the connection. Frees all resources associated with the corresponding connection.
Sqlsrv_commit commits the transaction.
Sqlsrv_configure Change the error handling and logging configuration.
Sqlsrv_connect Create a connection and open it.
Sqlsrv_errors returns error and/or warning information about the previous operation.
Sqlsrv_execute executes a predefined statement.
Sqlsrv_fetch makes the next row of data available for reading.
Sqlsrv_fetch_array retrieves the data for the next row in the form of a numeric index array, an associative array, or both of these arrays.
Sqlsrv_fetch_object retrieves the data for the next row as an object.
Sqlsrv_field_metadata returns the field metadata.
SQLSRV_FREE_STMT the closing statement. Frees all resources associated with the corresponding statement.
Sqlsrv_get_config returns the value of the specified configuration setting.
Sqlsrv_get_field retrieves the fields in the current row by index. You can specify the PHP return type.
Sqlsrv_has_rows detects if the result set has one or more rows.
Sqlsrv_next_result makes the next result available for processing.
Sqlsrv_num_rows the number of rows in the report result set.
Sqlsrv_num_fields retrieves the number of fields in the active result set.
SQLSRV_PREPARE prepares a Transact-SQL query, but does not execute the query. An implicit binding parameter.
SQLSRV_QUERY prepares a Transact-SQL query and executes it.
Sqlsrv_rollback rolls back the transaction.
Sqlsrv_rows_affected returns the number of rows that have been modified.
Sqlsrv_send_stream_data sends up to 8,000 bytes (8 KB) of data to the server each time the function is called.
Sqlsrv_server_info provides information about the server.

Take care of the hand, cough

PHP Connection SQLServer2008

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.