繁体   English   中英

从一个表中取出一列,在另一个表中搜索相同的列值,最后从第三个表中取出并显示数据

[英]Fetch a column from one table, search the same column value in another table and finally fetch and display data from the third table

是的,所以我有一个非常奇怪和复杂的问题(你甚至会从这个问题的标题中注意到)。 我有两个数据库: "admin""users" “admin”数据库中,我有一个名为“user_accounts”的表,其中一个列的名称是“course” ,它看起来像这样:

在此处输入图像描述

“用户”数据库中,我有 2 个名为“预定课程”“user_accounts”的表,其中我需要的列是“email”“user”“course_name” ,这就是它的样子:

在此处输入图像描述

在此处输入图像描述

所以,我真正想做的是从“user_accounts”表中获取“course”列的值,检查它是否与“booked_courses 表中“course_name”列中的记录相匹配。 如果确实匹配(在本例中为“业务基础”),则检查“booked_courses”表中“用户”列的值是什么(在本例中复制两封电子邮件),然后将这些记录与“电子邮件”匹配 user_accounts”表中的“列(这次在“users”数据库中检查它。

这是我试过的代码:

<?php

// Include the connection for the admin database

include_once 'handlers/db_conn_admin.php';

// Select and fetch the information from user_accounts table in the admin database

$sql = $conn->prepare("SELECT * FROM user_accounts WHERE username = ?");
$sql->bind_param("s", $username);

$username = $_SESSION['username'];

$sql->execute();
$result = $sql->get_result();
$trainer = mysqli_fetch_assoc($result);

// Now include the connection for the users database

include_once 'handlers/db_conn_users.php';

// Select and fetch the information from booked_courses table in the users database where the course_name is whatever is the value of "course" column in user_accounts table in the admin database

$sql_booked_courses = $conn->prepare("SELECT * FROM booked_courses WHERE course_name=?");
$sql_booked_courses->bind_param("s", $course_name);

$course_name = $trainer['course'];

$sql_booked_courses->execute();
$result_booked_courses = $sql_booked_courses->get_result();
$email = mysqli_fetch_assoc($result_booked_courses);

// Now select and fetch the information from user_accounts table in the users database where the email is whatever is the value of "user" column in booked_courses table in the users database

$sql_user_accounts = $conn->prepare("SELECT * FROM user_accounts WHERE email=?");
$sql_user_accounts->bind_param("s", $user);

$user = $email['user'];

$sql_user_accounts->execute();
$result_user_accounts = $sql_user_accounts->get_result();

// And now for the end make the loop to display all the information about the users of the selected email addresses (in this case "email@example.com" and "email2@example.com")

while($user_info = $result_user_accounts->fetch_assoc()) {

?>

<h1><?php echo $user_info['email'] ?></h1>

<?php

}

?>


是的,这确实有效,但是最后我只从“用户”数据库的“user_accounts”表中得到一个(最旧的)结果,当我需要获取所有 email 地址并将它们显示在HTML 页。 正如我猜测的那样,因为“mysqli_fetch_assoc”function 通常只给你一个值,所以我需要使用循环。 当我有 2 个具有相同课程列值的不同 email 地址时,问题就开始了。 我不太确定如何在没有循环的情况下将循环变成循环或如何保存获取的结果(在本例中为 2 email 地址),这样我就可以显示两行的信息,但我不能似乎为这种情况找到了任何解决方案。

我完全知道这可能听起来超级混乱、复杂和不必要,但是我已经在这些表上构建了整个系统,这是我要添加的小功能,我没有太多选择来组合表或类似的东西那。 如果您有任何解决方案,请有人帮我解决这个问题,并在评论中让我知道您可能需要的任何其他信息。

所以我实际上做的是在 while 循环中使用 while 循环,以免丢失从数据库中获取的任何结果:

// EVERYTHING STAYS THE SAME ABOVE THIS CODE

$sql_booked_courses = $conn->prepare("SELECT * FROM booked_courses WHERE course_name=?");
$sql_booked_courses->bind_param("s", $course_name);

$course_name = $trainer['course'];

$sql_booked_courses->execute();
$result_booked_courses = $sql_booked_courses->get_result();

// I ADDED THIS CODE:

while($emails = $result_booked_courses->fetch_assoc()) {

$email = $emails['user'];

// Now select and fetch the information from user_accounts table in the users database where the email is whatever is the value of "user" column in booked_courses table in the users database

$sql_user_accounts = $conn->prepare("SELECT * FROM user_accounts WHERE email=?");
$sql_user_accounts->bind_param("s", $user);

$user = $email['user'];

$sql_user_accounts->execute();
$result_user_accounts = $sql_user_accounts->get_result();

// And now for the end make the loop to display all the information about the users of the selected email addresses (in this case "email@example.com" and "email2@example.com")

while($user_info = $result_user_accounts->fetch_assoc()) {

?>

<h1><?php echo $user_info['email'] ?></h1>

<?php

} } // AND CLOSED THAT LOOP HERE (SO THERE IS A LOOP INSIDE A LOOP THAT WORKED

?>

我不确定这有多方便,但它工作得很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM