Search code examples
phpsqloopselectinner-join

Select Statement is loading the wrong data


I'm having a problem with my SQL SELECT statement and I searched on Google and youtube but I found nothing.

Here is my PHP code:

public function loadMark($data){
    dbConn(); 

    $query = "SELECT
        subject.subName
        , mark.mark
    FROM
        itcollage.marktype
        , itcollage.student
    INNER JOIN itcollage.parent 
        ON (student.pId = parent.pId)
    INNER JOIN itcollage.mark 
        ON (mark.stId = student.sId)
    INNER JOIN itcollage.subject 
        ON (mark.subId = subject.subId)
    WHERE 
        parent.pUsername='$data' 
    AND 
        mark.subId = subject.subId LIMIT 2";

    return $result=  mysql_query($query);

}

and I wan't this to load the correct marks for the parents that got the students ID, but it keeps on repeating the marks for all subjects.

Data as follows :

Subject      Mark
--------  | ------
Java 101  |   A
Java 101  |   D
Math 101  |   A
Math 101  |   D
  • Java 101 = A, Math = D

So whats wrong !!!

And here is my code for calling the class marks.class.php

    include '../classes/mark.class.php';

    $mark = new mark();
    $result = $mark->loadMark($user);
    $num = mysql_num_rows($result);

     if ($num == 0) {
?>
         <h2>No Marks for your Kid</h2>
<?php
     } else {
?>
         <h2>Marks</h2>
         <table>
             <thead>
                 <tr>
                     <th>Subject</th>
                     <th>Mark</th> 
                  </tr>
              </thead>
              <tbody>
<?php
                  while ($row = mysql_fetch_object($result)) {
                      $marks = $row->mark;
                      $subject = $row->subName;
?>

                      <tr class="light">
                          <td align="center"><?php echo $subject; ?></td>
                          <td align="center"><?php echo $marks; ?></td>
                      </tr>

 <?php
                   }
 ?>
               </tbody>   
           </table>

Can anyone show me what's wrong?


Solution

  • This looks like the problem. You don't specify a join condition for this table.

    FROM
        itcollage.marktype
        , itcollage.student
    

    Change to:

    FROM
        itcollage.marktype 
        INNER JOIN itcollage.student *(add join condition here)*