我有一台运行着几个大表的MSSQL服务器。我试着把它们放到一个HTML表中,这样我就可以在一个漂亮的CSS修改过的网页上显示所有的数据。我使用PHP将信息从SQL传送到我的HTML脚本,但是到目前为止,我发现生成这样一个表的惟一方法是将所有SQL列名硬编码到我的PHP-SQL查询中。
正如您在下面看到的,这是一个这样的表所需的设置。有没有更简洁的方法将SQL中的信息放到格式化的HTML表中?我已经四处寻找了几个小时,也许是为了寻找某种可以遍历所有SQL列的PHP脚本化循环,但我什么也没找到。我非常感谢大家的意见!
<table id="capacitors">
<thead>
<tr>
<td>Part Number</td>
<td>Capacitance</td>
<td>Capacitance Tolerance</td>
<td>Case Package</td>
<td>Case Package SI</td>
<td>Dielectric Char.</td>
<td>Dielectric Mat.</td>
<td>Halogen Free Stat.</td>
<td>Insulation Resistance</td>
<td>Lead Free Stat.</td>
<td>Lifecycle Stat.</td>
<td>Mounting Style</td>
<td>Operating Temp.</td>
<td>Packaging</td>
<td>Pin Count</td>
<td>Reach SVHC Comp.</td>
<td>Rohs Stat.</td>
<td>Size Height</td>
<td>Size Length</td>
<td>Size Thickness</td>
<td>Size Width</td>
<td>Temp. Coefficient</td>
<td>Voltage Rating DC</td>
</tr>
</thead>
<tbody>
<?php
foreach ($db->query($sql) as $rows){
?>
<tr>
<td><?php echo $rows['part_number']?></td>
<td><?php echo $rows['capacitance']?></td>
<td><?php echo $rows['capacitance_tolerance']?></td>
<td><?php echo $rows['case_package']?></td>
<td><?php echo $rows['case_package_si']?></td>
<td><?php echo $rows['dielectric_characteristic']?></td>
<td><?php echo $rows['dielectric_material']?></td>
<td><?php echo $rows['halogen_free_status']?></td>
<td><?php echo $rows['insulation_resistance']?></td>
<td><?php echo $rows['lead_free_status']?></td>
<td><?php echo $rows['lifecycle_status']?></td>
<td><?php echo $rows['mounting_style']?></td>
<td><?php echo $rows['operating_temperature']?></td>
<td><?php echo $rows['packaging']?></td>
<td><?php echo $rows['pin_count']?></td>
<td><?php echo $rows['reach_svhc_compliance']?></td>
<td><?php echo $rows['rohs_status']?></td>
<td><?php echo $rows['size_height']?></td>
<td><?php echo $rows['size_length']?></td>
<td><?php echo $rows['size_thickness']?></td>
<td><?php echo $rows['size_width']?></td>
<td><?php echo $rows['temperature_coefficient']?></td>
<td><?php echo $rows['voltage_rating_dc']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
发布于 2018-08-01 14:41:06
这是使用PHP Driver for SQL Server的基本示例
<?php
# Settings
$server = 'server\instance,port';
$database = 'database';
$user = 'user';
$password = 'password';
$tablename = 'tablename';
# Connection
$cinfo = array(
"Database" => $database,
"ReturnDatesAsStrings" => true,
"UID" => $user,
"PWD" => $password
);
$conn = sqlsrv_connect($server, $cinfo);
if($conn === false)
{
echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
exit;
}
# SQL statement
$sql = "SELECT * FROM [".$tablename."]";
$stmt = sqlsrv_prepare($conn, $sql);
if($stmt === false) {
echo "Error (sqlsrv_prepare): ".print_r(sqlsrv_errors(), true);
exit;
}
# Columns names
echo '<table id="'.$tablename.'">';
echo "<thead>";
echo "<tr>";
$metadata = sqlsrv_field_metadata($stmt);
foreach($metadata as $field) {
echo "<td>".$field['Name']."</td>";
}
echo "</tr>";
echo "</thead>";
# Table rows
echo "<tbody>";
if (!sqlsrv_execute($stmt)) {
echo "Error (sqlsrv_execute): ".print_r(sqlsrv_errors(), true);
exit;
}
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
echo "<tr>";
foreach($row as $value) {
echo "<td>".$value."</td>";
};
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
# End
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
?>
发布于 2018-08-01 04:57:16
可以使用SHOW COLUMNS
或DESCRIBE
$columns = "SHOW COLUMNS FROM <table>";
$output = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
echo $row['Field']."<br>";
}
https://stackoverflow.com/questions/51621902
复制相似问题