首页 > 解决方案 > 左连接后无法在 for-each 循环中显示列

问题描述

我正在我的项目表上运行此查询

SELECT item.*, status.status_id  FROM item LEFT JOIN status ON item.status = status.id

在此处输入图像描述

<?php foreach ($itemqedit as $myitem) : ?>
                                        <tr>
                                            <td><?= $myitem['item_desc'] ?></td>
                                            <td><?= $myitem['display'] ?></td>
                                            <td><?= $myitem['brand'] ?></td>
                                            <td><?= $myitem['model'] ?></td>
                                            <td><?= $myitem['spec'] ?></td>
                                            <td><?= $myitem['sn'] ?></td>
                                            <td><?= $myitem['set_name'] ?></td>
                                            <td><img src="items-img/<?= $myitem['item_pic'] ?>" style="width: 50px"></td>
                                            <td><?= $myitem['date_add'] ?></td>
                                            <td><?= $myitem['status_id'] ?></td>
                                            <td><a class="paginate_button current" href="itemedit.php?itmid=<?= $myitem['id'] ?>"> <button class="btn btn-info pull-left" > עריכה</button> </a> </td>
                                        </tr>

查询运行良好,我得到了想要的结果

我也在为每个循环运行这个查询,我可以显示除status_id来自status表的列之外的所有列

注意:未定义索引:第 94 行 C:\xampp\htdocs\ch10nb\edititem.php 中的 status_id

我究竟做错了什么?在 phpmyadmin 中,当我运行此查询时,我没有收到任何错误,我可以看到status_id已添加到项目表中,但我无法显示它。

标签: phpmysqlsqlmysqli

解决方案


尝试添加别名

SELECT item.*
    , status.status_id   AS  status_id
FROM item 
LEFT JOIN status ON item.status = status.id

推荐阅读