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
  • Version 4.0 requires Microsoft ODBC Driver 11 or Microsoft ODBC Driver 13.
  • Versions 3.2 and 3.1 of the driver require Microsoft ODBC Driver 11 (or higher). You can download the Microsoft ODBC Driver 11 here.
  • 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测试是否安装成功。


连接数据库测试:

$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);

?>



你可能感兴趣的:(mssql)