Echo contents of JOIN SQL tables with MySQLi

馋奶兔 提交于 2021-01-28 06:07:59

问题


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


回答1:


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>";
    }
}


来源:https://stackoverflow.com/questions/53028296/echo-contents-of-join-sql-tables-with-mysqli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!