我在网上看到的大多数示例都假设您知道要输出的特定列。但是如果你不这样做呢?这是我到目前为止使用SQLSRV
所得到的问题是:如果我想将所有SQL表显示到HTML表中,使列成为标题和其余数据行,该怎么办?
<?php
$serverName = "serverName\instanceName";
$connectionInfo = array( "Database"=>"dbName", "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo );
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
$sql = "SELECT * FROM SomeTable";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
echo "<table>";
echo "<thead>";
echo "<tr>";
foreach (sqlsrv_fetch_field($stmt) as $column){
echo "<th>$column</th>";
}
echo "</tr></thead>";
echo "<tbody>";
echo "<tr>";
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
echo "<td>$row</td>";
}
echo "</tr>";
echo "</tbody></table>";
sqlsrv_free_stmt( $stmt);
?>