PHP Connection mssql2008/2005 Database (SQLSRV) configuration instance _php tips

Source: Internet
Author: User
Tags sql server driver php database

This article describes the PHP connection mssql2008/2005 database (SQLSRV) configuration method, share for everyone to reference. The specific methods are as follows:

PHP connection mssql2008/2005 Database with the previous connection mssql2000 is not the same, the connection mssql2008/2005 is required to add their own PHP to the MSSQL Connection driver extension, and we commonly used Hp.ini extension= Php_mssql.dll extensions apply only to MSSQL2000, let's take a look at the solution here

1. Download extension

(1) Go to the official download a SQL Server Driver for PHP expansion pack, I am here to download the http://www.microsoft.com/en-us/download/details.aspx?id=20098 " Remember to download it as if you want to install first and then unzip.

(2) You can also directly from the site download (I previously downloaded, from Microsoft Official) "Click here to download directly"
After you unzip the downloaded RAR file, you'll get a bunch of. dll files

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, you can see, hehe)
Sqlserverdriverforphp_license.rtf
Sqlserverdriverforphp_readme.htm (Readme file)

2. Add extension

According to (VC6/VC9) need to choose the extension, my environment is Wamp (php5.2.6/apache2.2.8), I chose Php_sqlsrv_52_ts_vc6.dll,php_pdo_sqlsrv_52_ts_ Vc6.dll these two files, copied to the Wamp installation directory under the EXT directory, my ext directory is in wamp/bin/php/php5.2.6/ext/

3. Configure PHP.ini

(1) Add the following two extensions to the dynamic extensions of php.ini:
Extension=php_sqlsrv_52_ts_vc6.dll
Extension=php_pdo_sqlsrv_52_ts_vc6.dll
(2) to extension=php_pdo.dll the front of; remove, open PDO connect extension
(3) Restart Apache

4. Connection database (PDO connection)

Copy Code code as follows:
<?php
$servern = "Sfkfk27el8fj\sqltry";
$coninfo =array ("Database" => "Try2", "UID" => "sa", "PWD" => "123");
$conn =sqlsrv_connect ($servern, $coninfo) or Die ("Connection failed!");
$val =sqlsrv_query ($conn, "select * from usertable");
while ($row =sqlsrv_fetch_array ($val)) {
echo $row [1]. " <br/> ";
}
Sqlsrv_close ($conn);
?>

5. Examples

Link Example:
mssql_lib.php files are as follows:

Copy Code code as follows:
<?php
Class DB {
var $con = null;
function __construct ($dbhost, $dbuser, $dbpass, $dbname) {
$connectionInfo = Array ("UID" => $dbuser, "PWD" => $dbpass, "Database" => $dbname);
$this->con = Sqlsrv_connect ($dbhost, $connectionInfo);
}
function query ($sql) {
$result = sqlsrv_query ($this->con, $sql);
}
function GetRow ($sql) {
$result = sqlsrv_query ($this->con, $sql);
$arr = Array ();
while ($row = Sqlsrv_fetch_array ($result))
{
$arr [] = $row;
}
return $arr [0];
}
function GetAll ($sql) {
$result = sqlsrv_query ($this->con, $sql);
$arr = Array ();
while ($row = Sqlsrv_fetch_array ($result))
{
$arr [] = $row;
}
return $arr;
}
function __destruct () {
Unset ($con);
}
}

The test.php page is as follows:

Copy Code code as follows:
Simple call
$db = new db (Db_host, Db_user, Db_pass, db_name);
$sql = "SELECT * from Crm_order_batch where (status=0 or status is null) and lock_id are not NULL";
$orders _add_list = $db->getall ($sql);

I hope this article will help you with your PHP database program design.

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.