将PHP连接到Ms SQL

时间:2018-06-07 09:22:20

标签: php sql-server xampp

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

1 个答案:

答案 0 :(得分:0)

您可以使用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();