我已使用PHP连接到我的MS SQL Server 2008 R2。除了输出显示两次之外,一切正常。
以下是用于建立连接的PHP代码。任何帮助表示赞赏。
<?php
$hostname = "localhost\FAIZ2";
$username = "sa";
$password = "11235813";
$database = "Test";
$connectioninfo = array("UID"=>$username,"PWD"=>$password,"Database"=>$database);
$link = sqlsrv_connect($hostname,$connectioninfo) or die( print_r( sqlsrv_errors(), true));
$sql = "SELECT * FROM StudentData";
$result = sqlsrv_query($link,$sql) or die("Unable to select: ".sqlsrv_errors());
print "<table>\n";
while($row = sqlsrv_fetch_array($result)) {
print "<tr>\n";
foreach($row as $field) {
print "<td>$field</td>\n";
}
print "</tr>\n";
}
print "</table>\n";
sqlsrv_close($link);
?>
答案 0 :(得分:0)
默认情况下,sqlsrv_fetch_array()
返回一个包含数字键和命名字段键的数组。因此每个列值返回两次。所以你最终会得到像......这样的行。
[0] => 123
['id'] => 123
[1] => 'Bobby'
['name'] => 'Bobby'
[2] => '1994-03-03'
['date_of_birth'] => '1994-03-03'
将SQLSRV_FETCH_ASSOC
或SQLSRV_FETCH_NUMERIC
参数添加到sqlsrv_fetch_array
,如下所示:
$row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)