SQL Query: Adding total hours
<p>I currently have 5 tables in MySQL database. Some of them share foreign keys and are interdependent of each other. I am displaying classes accordingly to their majors. I have two majors: Computer Engineer and Information System. Their values are stored in a table named major. Their respective ID is 1 and 2. I  am having difficulties totaling the hours of all course. How can I add each course hours and show the total hours for the major at the bottom of the page?</p>

<pre><code>&lt;?


try {

    $pdo = new PDO ("mysql:host=$hostname;dbname=$dbname","$username","$pw");
    } catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e-&gt;getMessage() . "\n";
    exit;
  }
      $query = $pdo-&gt;prepare("SELECT course.name, course.code, course.description, course.hours, 
                             CASE semester.semester
                             WHEN 0 THEN 'Spring'
                             WHEN 1 THEN 'Fall'
                             WHEN 2 THEN 'All-year'
                             END semester, semester.year
                             FROM course
                             LEFT JOIN major_course_xref ON course.id = major_course_xref.course_id
                             LEFT JOIN major ON major.id = major_course_xref.major_id
                             LEFT JOIN course_semester_xref ON course.id = course_semester_xref.course_id
                             LEFT JOIN semester ON course_semester_xref.semester_id = semester.id
                             Where major.id = '1'
                             ");
      $query-&gt;execute();

     if ($query-&gt;execute()){

      while ($row = $query-&gt;fetch(PDO::FETCH_ASSOC)){  
        print "&lt;b&gt;" . $row['name'] . "&lt;/b&gt;&lt;br&gt;";
        print $row['code'] . "&lt;br&gt;";
        print $row ['description'] . "&lt;br&gt;";
        print $row['hours'] . " hrs. &lt;br&gt;";
        print $row['semester'] . "&lt;br&gt;";
        print $row['year'] . "&lt;br&gt;";
         print "------------------------------&lt;br /&gt;";
    }


    }
else
    echo 'Could not fetch results.';

      unset($pdo); 
      unset($query);

?&gt;      
</code></pre>

<p><strong>Current Output</strong></p>

<pre><code>Computer Programming I
CPSC1400
Introduction to disciplined, object-oriented program development.
4 hrs. 
Spring
2013
------------------------------
Computer Programming II
CPSC1500
This course builds upon the topics covered in Computer Science I and provides experience developing complex applications. 
4 hrs. 
Fall
2013
------------------------------
Database Programming
CPSC2100
Study of relational database management systems and information storage and retrieval techniques. 
4 hrs. 
Spring
2014
</code></pre>
