Search code examples
phpinner-joinmysqli

Echo contents of JOIN SQL tables with MySQLi


I'm working on a system, and this module is supposed to echo the contents of the database.

It worked perfectly until I added some JOIN statements to it.

I've checked and tested the SQL code, and it works perfectly. What's not working is that part where I echo the content of the JOINed table.

My code looks like this:

$query = "SELECT reg_students.*, courses.*
          FROM reg_students
          JOIN courses ON reg_students.course_id = courses.course_id
          WHERE reg_students.user_id = '".$user_id."'";

$result = mysqli_query($conn, $query);
if (mysqli_fetch_array($result) > 0) {
    while ($row = mysqli_fetch_array($result)) {
       echo $row["course_name"]; 
       echo $row["course_id"];

The course_name and course_id neither echo nor give any error messages.


UPDATE: I actually need to increase the query complexity by JOINing more tables and changing the selected columns. I need to JOIN these tables:

tutors which has columns: tutor_id, t_fname, t_othernames, email, phone number
faculty which has columns: faculty_id, faculty_name, faculty_code
courses which has columns: course_id, course_code, course_name, tutor_id, faculty_id

I want to JOIN these tables to the reg_students table in my original query so that I can filter by $user_id and I want to display: course_name, t_fname, t_othernames, email, faculty_name


Solution

  • I can't imagine that the user_info table is of any benefit to JOIN in, so I'm removing it as a reasonable guess. I am also assuming that your desired columns are all coming from the courses table, so I am nominating the table name with the column names in the SELECT.

    For reader clarity, I like to use INNER JOIN instead of JOIN. (they are the same beast)

    Casting $user_id as an integer is just a best practices that I am throwing in, just in case that variable is being fed by user-supplied/untrusted input.

    You count the number of rows in the result set with mysqli_num_rows().

    If you only want to access the result set data using the associative keys, generate a result set with mysqli_fetch_assoc().

    When writing a query with JOINs it is often helpful to declare aliases for each table. This largely reduces code bloat and reader-strain.

    Untested Code:

    $query = "SELECT c.course_name, t.t_fname, t.t_othernames, t.email, f.faculty_name
              FROM reg_students r
              INNER JOIN courses c ON r.course_id = c.course_id
              INNER JOIN faculty f ON c.faculty_id = f.faculty_id
              INNER JOIN tutors t ON c.tutor_id = t.tutor_id
              WHERE r.user_id = " . (int)$user_id;
    if (!$result = mysqli_query($conn, $query)) {
        echo "Syntax Error";
    } elseif (!mysqli_num_rows($result)) {
        echo "No Qualifying Rows";
    } else {
        while ($row = mysqli_fetch_assoc($result)) {
            echo "{$row["course_name"]}<br>";
            echo "{$row["t_fname"]}<br>";
            echo "{$row["t_othernames"]}<br>";
            echo "{$row["email"]}<br>";
            echo "{$row["faculty_name"]}<br><br>";
        }
    }