【发布时间】:2018-08-18 14:44:20
【问题描述】:
我正在努力使用 while 循环从表 b 中获取相应的值。我的数据库中有以下数据:
表 A
数字 - 实体
3000 - ent1
3010 - ent1
4000 - ent1表 B
数字 - 实体
3000 - 10
3010 - 10
3010 - 20
4000 - 20
3000 - 30
4000 - 30
现在,我需要数据来输出下表,其中第一列来自表 a,接下来的列来自表 b:
ent1 - 10 - 20 - 30
3000 - 3000 - 空 - 3000
3010 - 3010 - 3010 - 空
4000 - 空 - 4000 - 4000
我尝试过组合两个 WHILE 循环,但没有成功:
$query_entity = "SELECT number, entity FROM table_a ORDER BY number ASC";
$result_entity = mysqli_query($mysqli, $query_entity);
while ($entities = mysqli_fetch_array($result_entity)) {
$entitiesAccount = $entities['number'];
$query_entity_tabtwo = "SELECT number, entity
FROM table_b
WHERE number = $entitiesAccount";
$result_entity_tabtwo = mysqli_query($mysqli, $query_entity_tabtwo);
while ($entities_tabtwo = mysqli_fetch_array($result_entity_tabtwo)) {
echo $entitiesAccount . " - " . $entities_tabtwo['number'];
}
}
我得到的结果不是我想要的上述结果,因为结果没有分隔表 b 中的“实体”字段。如何更改我的脚本以获得所需的结果?
【问题讨论】:
-
我需要数据来输出下表 你的意思是需要渲染一个HTML表格吗?导致获取一组数据并渲染它可能是两个不同的进程,每个进程都有完全不同的方法。
-
9KSoft;是的,请考虑以上示例作为数据的填充方式。
标签: php mysqli while-loop