Skip to content
Advertisement

I want to show or fetch data from two tables [closed]

I want to show or fetch teacher’s name but it always fetch only the teacher’s id, Thanks for your help.

This is my code:

 <?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>

Here is Teacher Class Table on Data base Teacher Class Table on Data base

Here, the Teacher table Teacher table on Data base

And the Assign Teacher Table Assign Teacher Table

Advertisement

Answer

If you are displaying values from multiple tables, you need to join the tables and then only you can display them.

Do read about https://www.w3schools.com/sql/sql_join.asp to get an idea of SQL JOINS.

Try the below code.

$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>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement