我想从两个表中显示或获取数据

我想显示或获取老师的名字,但它总是只获取老师的 id,谢谢你的帮助。


这是我的代码:


 <?php

     include('../dbcon.php');


     $query=mysqli_query($connection,"select * from `teacher_class`");

     while($row=mysqli_fetch_array($query)){

        ?>

        <tr>


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

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

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

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

这是数据库上的教师班级表

http://img2.mukewang.com/6353ac8100013cb006570289.jpg

这里是教师桌

http://img.mukewang.com/6353ac8d000192e206570303.jpg

和分配教师表 分配教师表


catspeake
浏览 102回答 2
2回答

墨色风雨

如果您要显示来自多个表的值,则需要连接这些表,然后只有您可以显示它们。请阅读https://www.w3schools.com/sql/sql_join.asp以了解 SQL JOINS。试试下面的代码。$query=mysqli_query($connection,"SELECT * FROM 'teacher' AS teacher INNER JOIN teacher_class ON teacher.teacher_id = teacher_class.teacher_id");while($row=mysqli_fetch_array($query)){?><tr><td><?php echo $row['class_id']; ?></td><td><?php echo $row['school_year']; ?></td><td><?php echo $row['firstname'].' '.$row['mname'].' '.$row['lastname']; ?></td><td><?php echo $row['subject_id']; ?></td>

偶然的你

这是您想要的查询SELECT&nbsp;tc.teacher_id,&nbsp;t.teacher_name,&nbsp;tc.subject_id&nbsp;FROM&nbsp;teacher_class&nbsp;AS&nbsp;tc&nbsp;LEFT&nbsp;JOIN&nbsp;teacher&nbsp;ON&nbsp;t.teacher_id&nbsp;=&nbsp;tc.teacher_id
打开App,查看更多内容
随时随地看视频慕课网APP