php - 如何打印表格的所有结果而不需要逐个变量打印?
问题描述
使用数据表的相应列名在两列表中打印所有这些结果的一些更简单的方法,而无需逐个打印变量。
我正在使用 mysqli 面向对象的 PHP,而不是使用 PDO,请不要这样:(fetchAll(PDO::FETCH_ASSOC);
)。
$stmt->execute();
$stmt->store_result();
$stmt->bind_result(
$id_logtrama, $fechaHora, $idCliente,
$idEquipo, $statusGlobal, $temp,
$tempAmb, $hrelAmb, $presion,
$termopar1, $termopar2, $volt_fase1,
$amps_fase1, $frec_fase1, $fpot_fase1,
$volt_fase2, $amps_fase2, $frec_fase2, $fpot_fase2,
$volt_fase3, $amps_fase3, $frec_fase3, $fpot_fase3,
$status_temp, $status_tempAmb, $status_hrelAmb, $status_presion,
$status_termopar1, $status_termopar2, $status_volt_fase1,
$status_amps_fase1, $status_frec_fase1, $status_fpot_fase1,
$status_volt_fase2, $status_amps_fase2, $status_frec_fase2,
$status_fpot_fase2, $status_volt_fase3, $status_amps_fase3,
$status_frec_fase3, $status_fpot_fase3, $trama
);
while ($stmt->fetch()) {}
解决方案
我假设这是一行有很多列,但是即使您有多行,也可以使用获取结果集$res = $stmt->store_result();
,然后您可以将该行作为 assoc 数组获取,并在内部 foreach 循环中使用其名称和值处理每一列,例如这个
$stmt->execute();
$res = $stmt->store_result();
?>
<table class="table table-bordered">
<?php
# Fetch each row from resultset
while ( $row = $res->fetch_assoc() ){
# for each row process each column
foreach ($row as $name => $val ) {
echo "
<tr>
<td width='30%'><label>$name</label></td>
<td width='70%'>$val</td>
</tr>";
}
}
推荐阅读
- python - 为什么 multiprocessing.Process 不能调用对象实例的方法?无法理解泡菜的问题
- django - 将 Django-Rest-Framework 序列化程序从平面更改为嵌套?
- android - 来自 getContentResolver() 的游标空对象
- javascript - 在 Javascript 中的特定时间对音频样本进行编码
- java - 如何将旧数据库中的数据获取到具有不同列设置的新数据库?
- java - 如何编码 GZipEncoder.class 编码的 JSON 响应
- wordpress - 自定义帖子类型重写 slug
- c# - 我可以以编程方式打开“灯泡建议”并调用“添加使用语句...”吗?
- pandas - 如何更新包含来自 pandas df 的自动编号列的 MS Access 表?
- blueprism - 如何在 BluePrism 中使用 Mapiex 保存电子邮件中的附件?