php 連線 SQL Server 2008
PHP自帶的MSSQL擴充套件php_mssql.dll是給SQL Server 2000用的,SQL Server 2000+的版本用微軟為PHP提供的第三方擴充套件:Microsoft SQL Server PHP 驅動程式,URL是:https://msdn.microsoft.com/library/dn865013.aspx ,下載版本參照以下(注意:如果是3.1+版本還需要下載安裝ODBC):
-
Version support for PHP is as follows
- Version 4.0 supports PHP 7.0+
- Version 3.2 supports PHP 5.6, 5.5, and 5.4
- Version 3.1 supports PHP 5.5 and 5.4
- Version 3.0 supports PHP 5.4.
For more detail and for supported operating systems, see System Requirements (Microsoft Drivers for PHP for SQL Server) . - An Internet Information Services (IIS) Web server is required
-
Versions 3.2 and 3.1 of the driver require Microsoft ODBC Driver 11 (or higher). You can download the
- Version 3.0 requires the x86 version of Microsoft SQL Server 2012 Native Client.
比如下載SQLSRV31.EXE檔案雙擊解壓得到N個*.dll檔案,根據php安裝目錄包含的類似“php5ts.dll”來使用對應的*.ts.dll或*.nts.dll,參考如下URL:https://msdn.microsoft.com/en-us/library/cc296170(v=sql.105).aspx 。
配置:
把 php_sqlsrv_55_ts.dll 和 php_pdo_sqlsrv_55_ts.dll 一起拷貝到php安裝目錄的擴充套件目錄(ext)下,配置php.ini檔案:增加以下兩行:
extension=php_sqlsrv_55_ts.dll
extension=php_pdo_sqlsrv_55_ts.dll
重啟相關服務後用phpinfo測試是否安裝成功。
連線資料庫測試:
<?php
$serverName = "MS-201703.....GD\SQLEX....."; //伺服器名稱,在 sql server management studio 的登入介面檢視
$uid = "sa"; //資料庫使用者名稱
$pwd = "123"; //資料庫密碼
$db = "cart"; // 資料庫名
$connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>$db);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn == false) {
echo "連線失敗!";
die( print_r( sqlsrv_errors(), true));
}
//執行有結果集的SQL語句
$query = sqlsrv_query($conn, "select top 1 * from tb_goods");
$row = sqlsrv_fetch_array($query) ;
print_r($row);
?>