简体   繁体   English

内部联接表并显示在php中

[英]Inner Join tables and display in php

I want to select data from more tables with Inner join. 我想通过内部联接从更多表中选择数据。

These are my tables. 这些是我的桌子。

teams (id, team_name)
badges (id, badgename, badgeimage, badgedescription)
teambadges (id, team_id, badge_id)

I want to write a statement that shows the team name with all the badges they have. 我想写一个声明,显示团队名称以及他们拥有的所有徽章。 I also want to display this in a table 我也想在表格中显示

This is my statement. 这是我的声明。

$sql = mysqli_query($connection, 'SELECT teams.team_name,badges.badgename
FROM teambadges
INNER JOIN teams ON teams.id = teambadges.team_id
INNER JOIN badges ON badges.id = teambadges.badge_id;');

Php: Php:

<table class="table table-condensed table-striped table-bordered table-hover">
            <thead>
                <tr>
                    <th width="5%"><center>No</center></th>
                    <th>team id</th>
                    <th>badge id</th>


                </tr>
            </thead>
            <tbody id="data">
            <?php $no=1; while ($row = mysqli_fetch_array($sql)) { ?>
                <tr>
                    <td align="center"><?php echo $no; ?></td>
                    <td><?php echo $row['team_name']; ?></td>
                    <td><?php echo $row['badgename']; ?></td>

                </tr>
            <?php $no++; } ?>   
            </tbody>
        </table>

This is executed inside the php page but i keep getting this error : Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, 这是在php页面内执行的,但我一直收到此错误:警告:mysqli_fetch_array()期望参数1为mysqli_result,

使用mysqli_query()或die(mysqli_error($ connection))来获取错误并检查查询是否在mysql上成功运行

i think you error is here 我认为你的错误在这里

<td><?php echo $row['teams.name']; ?></td>
<td><?php echo $row['badges.name']; ?></td>

just chech if you have the right name column . 如果您有正确的名称列,请检查。 if you have there table with the same name 如果那里有同名的桌子

teams (id, team_name)
badges (id, badgename, badgeimage, badgedescription)
teambadges (id, team_id, badge_id)

you must do this : 您必须这样做:

<td><?php echo $row['team_name']; ?></td>
    <td><?php echo $row['badgename']; ?></td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM