首页 > 解决方案 > 将 PHP 连接到 SQL 女士

问题描述

我已经完成了在 PHP 和 Ms SQL 之间建立连接的所有步骤,但它仍然无法正常工作并显示错误“致命错误:未捕获的错误:调用未定义的函数 sqlsrv_connect()”。我也安装了 MS SQL 驱动程序。我在 php.ini 中使用的扩展现在是 php_pdo_sqlsrv_53_ts_vc9.dll,但它不包含在 xampp 扩展中。TCP/IP 也已启用。

标签: phpsql-serverxampp

解决方案


您可以使用 PDO 语句连接下面的示例:

# Connecting to PostGreSQL
$dbh = new PDO("msql:dbname=$dbname; host=$host", $username, $password);
# Setting an attribute on the database handle and error reporting
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
# Prepares a statement for execution and returns a statement object 
$query = 'SELECT * FROM test';
$stmt = $dbh->prepare($query);
$stmt->execute();
# Set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
# Returns an array containing all the results from the query
$allRows = $stmt->fetchAll();

推荐阅读