0

table course[name,departament,year,id,examinar] for example:

music,music Science,first,1,pr.Elias

table grade[studentname,stlastname,departament,,course,yearOfcourse,grade,examinar] for example:

roland,doda,music Science,music,first,10,pr.Elias

Now i have the below code

$query = "SELECT * FROM course where  year ='first' ";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result) ){
    echo "<tr>
        <td>".$row['name']."</td>
        <td>".$row['departament']."</td>
        <td>".$row['examinar']."</td> 
        </tr>";
} 

This works fine but that i want is into <tr> tags of while loop to echo out the grade from table grade?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
dorina
  • 1
  • 3

1 Answers1

0

This might do what you want, but I have had so make a few assumptions in the columns required to make a correct join.

I also added some error checking so that if I made an incorrect assumption you will be told about it.

$query = "SELECT c.*,g.grade 
          FROM course c 
              JOIN grade g ON (     g.departament = c.departament
                                AND g.year = c.year
                                AND g.course = c.name                        
                            )
          WHERE c.year ='first' ";

$result = mysql_query($query);
if ( $result === false ) {
    echo mysql_error();
    exit
}

while ($row = mysql_fetch_array($result) ){
    echo "<tr>
        <td>{$row['name']}</td>
        <td>{$row['departament']}</td>
        <td>{$row['examinar']}</td> 
        <td>{$row['grade']}</td> 
        </tr>";
} 

Please dont use the mysql_ database extension, it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the PDO database extensions. Start here its really pretty easy

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149