PHP Connect MSSQL2008 and 2005 Database of SQLSRV Configuration Instance

  • 2021-07-22 09:18:56
  • OfStack

This article describes the example of PHP connection MSSQL 2008/2005 database (SQLSRV) configuration method, to share for your reference. The specific methods are as follows:

The connection between PHP and MSSQL2008/2005 database is different from the previous connection between mssql2000. To connect mssql2008/2005, we need to add PHP driver extension to MSSQL connection. However, extension=php_mssql. dll extension in hp. ini is only applicable to connect MSSQL2000. Let's take a look at the solution to this problem

1. Download extensions

(1) Download an extension package of SQL Server Driver for PHP officially. I downloaded http://www.microsoft.com/en-us/download/details.aspx here. id=20098 "Remember that after downloading, it seems that you should install it first and then unzip it"

(2) You can also download it directly from this site (I downloaded it before, from microsoft official) [Click here to download it directly]
Unzip the downloaded rar file and you will get a heap of. dll files

Download the driver, install the release program after downloading, 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, if English is good enough, you can see it, hehe)
SQLServerDriverForPHP_License.rtf
SQLServerDriverForPHP_Readme. htm (ReadMe)

Step 2 Add extensions

Extensions are selected based on (vc6/vc9). My environment is WAMP (php5.2. 6/apache2.2. 8). I have selected two files, php_sqlsrv_52_ts_vc6.dll and php_sqlsrv_52_ts_vc6.dll, copied to the ext directory under the wamp installation directory. My ext directory is in wamp/bin/php/php5.2. 6/ext/

3. Configure php. ini

(1) Add the following two extensions to Dynamic Extensions of php. ini:
extension=php_sqlsrv_52_ts_vc6.dll
extension=php_pdo_sqlsrv_52_ts_vc6.dll
(2) Will; extension=php_pdo. dll preceding; Remove and open pdo connection extension
(3) Restart apache

4. Connecting to the database (pdo connection)

<?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

Example link:
The file mssql_lib. php is 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 reads 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 is not null  ";
$orders_add_list = $db->getAll($sql);

I hope this article is helpful to everyone's PHP database programming.


Related articles: