PHP connecting to MSSQL2008/2005 database is different from the previous connection to mssql2000. To connect to mssql2008/2005, you need to add PHP driver extension for MSSQL connection, and we commonly use extension=php_mssql.dll in hp.ini The extension is only suitable for connecting to MSSQL2000. Let's take a look at the solution to this.
1. Download the extension
(1) Go to the official website to download an expansion package for SQL Server Driver for PHP. I downloaded it here http://www.microsoft.com/en- us/download/details.aspx?id=20098 [Remember that after downloading, you need to 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 directly]
After decompressing the downloaded rar file, you will get a bunch of .dll files
Download the driver, and install the release program after downloading, which contains 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 your English is good enough, you can read it, hehe)
SQLServerDriverForPHP_License.rtf
SQLServerDriverForPHP_Readme.htm (readme file)
2. Add extensions
Choose extensions according to (vc6/vc9) needs. My environment is WAMP (php5.2.6/apache2.2.8), I chose the two files php_sqlsrv_52_ts_vc6.dll and php_pdo_sqlsrv_52_ts_vc6.dll and copied them to the ext directory in 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 the Dynamic Extensions of php.ini:
extension=php_sqlsrv_52_ts_vc6.dll
extension=php_pdo_sqlsrv_52_ts_vc6.dll
( 2) Remove the ; in front of ;extension=php_pdo.dll and enable the pdo connection extension
(3) Restart apache
4. Connect to the database (pdo connection)
<?php $servern="SFKFK27EL8FJ\SQLTRY"; $coninfo=array("Database"=>"try2","UID"=>"sa","PWD"=>"123"); $conn=sqlsrv_connect($servern,$coninfo) or die ("连接失败!"); $val=sqlsrv_query($conn,"select * from usertable"); while($row=sqlsrv_fetch_array($val)){ echo $row[1]."<br />"; } sqlsrv_close($conn); ?>
5. Example
Link example:
mssql_lib.php file 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); } }
test.php page is as follows:
//简单调用 $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);
Related recommendations:
How to operate MSSQL query data paging,
php method to connect to MSSQL
MySQL database source command detailed explanation_MsSql
The above is the detailed content of PHP connects to MSSQL database (SQLSRV) instance. For more information, please follow other related articles on the PHP Chinese website!