如何使用 xampp 将 sql server 与 php 连接?
我正在尝试使用 Xampp 将我的 SQL 服务器与 PHP 连接.我已经在 ext 文件夹中上传了 dll 文件,但我无法连接它.我的 PHP 版本是 7.2.6.上传的 dll 文件是 - php_pdo_sqlsrv_72_ts.dll、php_sqlsrv_72_ts.dll.我写了这段代码来用 PHP 连接我的 SQL 数据库-
I am trying to connect my SQL server with PHP using Xampp. I have already uploaded dll files in the ext folder but I am unable to connect it. My PHP version is 7.2.6. Uploaded dll files are - php_pdo_sqlsrv_72_ts.dll, php_sqlsrv_72_ts.dll. I have written this code to connect my SQL database with PHP-
<?php
$serverName = "INDO-SERVSQLEXPRESS,1443";
$uid = "sa";
$pwd = "XXXXXX";
$databaseName = "web";
$connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database"=>$databaseName);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn )
{
echo "Connection established.
";
}
else
{
echo "Connection could not be established.
";
die( print_r( sqlsrv_errors(), true));
}
sqlsrv_close( $conn);
?>
当我尝试这个时出现这个错误-
I am getting this error when I had tried this-
致命错误:未捕获的错误:调用未定义的函数C:xampphtdocsiometricdb.php:7 中的 sqlsrv_connect() 堆栈跟踪:#0{main} 在第 7 行的 C:xampphtdocsiometricdb.php 中抛出.
Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect() in C:xampphtdocsiometricdb.php:7 Stack trace: #0 {main} thrown in C:xampphtdocsiometricdb.php on line 7.
任何人都知道我做错了什么或如何连接数据库.
Anyone has an idea where I am doing wrong or how to connect with the database.
推荐答案
安装 PHP Driver for SQL Server(sqlsrv
和/或 pdo_sqlsrv
PHP 扩展)可以完成执行后续步骤:
Installation of PHP Driver for SQL Server (sqlsrv
and/or pdo_sqlsrv
PHP extensions) can be done following the next steps:
- 基于 Microsoft PHP Drivers for SQL Server Support Matrix 下载此驱动程序的适当版本.就您而言 - 版本 5.2 或 5.3(32 位或 64 位也取决于 PHP 版本).
- 下载并安装适当的 ODBC 驱动程序 - 请参阅 Microsoft Drivers for PHP for SQL Server 的系统要求
- 加载 SQL Server 的 PHP 驱动程序作为 PHP 扩展.
- 重启 Apache
- Based on Microsoft PHP Drivers for SQL Server Support Matrix download appropriate version of this driver. In your case - version 5.2 or 5.3 (32-bit or 64-bit also depends on PHP version).
- Download and install an appropriate ODBC driver - see System Requirements for the Microsoft Drivers for PHP for SQL Server
- Load PHP Driver for SQL Server as PHP extension.
- Restart Apache
使用检查配置.应该有一个名为
pdo_sqlsrv
(如果你使用 PDO)和/或 sqlsrv
(没有 PDO)的部分.
Check the configuration with <?php phpinfo();?>
. There should be a section with name pdo_sqlsrv
(if you use PDO) and/or sqlsrv
(without PDO).
相关文章