user1434156
user1434156

Reputation:

SQL JOIN query not displaying anything- PHP

I am doing a sql query but it is not returning any data from the tables. I am not sure why, I have changed from INNER to LEFT but no luck. I have revised the query several times but i cant find an issue. Any thoughts of why I am not getting anything displayed?

TABLE STRUCTURE

TABLE CREATE QUERY

PHP

$query = ("SELECT  class.class_name, class.class_caption, class.class_credit_hours, class.class_description, faculty_fname, faculty_lname
            FROM class
            LEFT JOIN section 
            ON class.id = section.class_id
            LEFT JOIN faculty 
            ON faculty.id = section.faculty_id OR faculty.id = office_hours.faculty_id
            LEFT JOIN faculty_titles
            ON faculty_titles.faculty_id = faculty.id
            LEFT JOIN faculty_education
            ON faculty_education.faculty_id = faculty.id 
            LEFT JOIN major_class_br
            ON major_class_br.class_id = class.id
            LEFT JOIN major_minor 
            ON major_class_br.major_minor_id = major_minor.id
            LEFT JOIN sched_sect_br 
            ON sched_sect_br.section_id =  section.id
            LEFT JOIN schedule
            ON schedule.id = sched_sect_br.schedule_id
            LEFT JOIN semester
            ON semester.id = schedule.semester_id
            LEFT JOIN office_hours
            ON schedule.id = office_hours.schedule_id AND faculty.id = office_hours.faculty_id       
                 ");
      //execute query
      $result = mysql_query($query);

     if ($result){

    $totalhours = 0;

    while ($row = mysql_fetch_assoc( $result ))
    {  
        print "<b>" . $row['class_name'] . "</b><br>";
        print $row['class_caption'] . "<br>";
        print $row ['class_credit_hours'] . "hrs. <br>";
            print $row ['faculty_lname'] . "hrs. <br>";
            print $row ['faculty_fname'] . "hrs. <br>";
        print $row['class_description'] . "<br>";      
        print "------------------------------<br />";
        $totalhours += $row['class_credit_hours']; 
    }   
    }

    print "<p>Total hours for Major: " . $totalhours . ".</p>";

Desired display:

Computer Programming I
CP1000
4
James Doe
This course offers introduction to programming. 

UPDATE: The issue was found here but I am not sure why

ON faculty.id = section.faculty_id OR faculty.id = office_hours.faculty_id

Upvotes: 0

Views: 314

Answers (1)

Gapton
Gapton

Reputation: 2124

  • Copy the SQL, go to MySQL admin, execute the SQL there and see if it returns any results.

  • Remove all the JOIN and execute again, you should see at least something

  • Add the JOIN one by one

  • Try to identify which JOIN resulted in no rows being returned

  • Check the left and right hand side of that join and ensure there actually are valid data to join

Alternatively use OUTER JOIN

Upvotes: 1

Related Questions