1 回答
TA贡献2019条经验 获得超9个赞
您正在使用来自两个不同 PHP 扩展(sqlsrv_connect()和 mysqli_query()\ mysqli_fetch_array())的函数。要连接到 SQL Server,您只需要sqlsrv_函数(SQL Server 的 PHP 驱动程序的一部分)。
以下脚本根据您的尝试演示了如何连接到 SQL Server 并检索数据:
<?php
// Connection
$serverName = "tcp:DBName.database.windows.net,1443";
$connectionOptions = array("Database" => "DBName", "Uid" => "DBUser", "PWD" => "DBpwd");
$connection = sqlsrv_connect($serverName, $connectionOptions);
if ($connection === false) {
echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
exit;
}
// Statement
$tsql = "SELECT Name, Age FROM Person";
$query = sqlsrv_query($connection, $tsql);
if ($query === false) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
?>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tbody>
<?php
// Data
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
?>
<tr>
<td><?php echo $row['Name'] ?></td>
<td><?php echo $row['Age'] ?></td>
</tr>
<?php
}
?>
</tbody>
<?php
// End
sqlsrv_free_stmt($query);
sqlsrv_close($connection);
?>
- 1 回答
- 0 关注
- 117 浏览
添加回答
举报