[英]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.在我向其中添加了一些 JOIN 语句之前,它运行良好。
I've checked and tested the SQL code, and it works perfectly.我已经检查并测试了 SQL 代码,它运行良好。 What's not working is that part where I echo the content of the JOINed table.
不起作用的是我回显 JOINed 表内容的那部分。
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. course_name 和 course_id 既不回显也不给出任何错误消息。
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
有列的
tutors
: tutor_id
、 t_fname
、 t_othernames
、 email
、 phone number
faculty
which has columns: faculty_id
, faculty_name
, faculty_code
faculty
: faculty_id
、 faculty_name
、 faculty_code
courses
which has columns: course_id
, course_code
, course_name
, tutor_id
, faculty_id
courses
: 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
我想在我的原始查询
reg_students
这些表加入reg_students
表,以便我可以按$user_id
进行过滤,并且我想显示: course_name
、 t_fname
、 t_othernames
、 email
、 faculty_name
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.我无法想象
user_info
表对 JOIN 有任何好处,因此我将其删除为合理的猜测。 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.我还假设您想要的列都来自
courses
表,所以我在 SELECT 中用列名指定表名。
For reader clarity, I like to use INNER JOIN
instead of JOIN
.为了读者清晰起见,我喜欢使用
INNER JOIN
而不是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.将
$user_id
为整数只是我提出的最佳实践,以防万一该变量由用户提供/不受信任的输入提供。
You count the number of rows in the result set with mysqli_num_rows()
.您使用
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()
.如果只想使用关联键访问结果集数据,请使用
mysqli_fetch_assoc()
生成结果集。
When writing a query with JOINs it is often helpful to declare aliases for each table.使用 JOIN 编写查询时,为每个表声明别名通常很有帮助。 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>";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.