Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 试图显示两个表中的数据_Php_Mysql - Fatal编程技术网

Php 试图显示两个表中的数据

Php 试图显示两个表中的数据,php,mysql,Php,Mysql,我正试图根据在另一个表中找到的id从一个表中获取数据。我的第一个表格叫做user\u thinks,这个表格保存了用户在我的“社交媒体网站”上发表的所有公开帖子的数据。我还有一个名为users的表,它存储了站点注册用户的所有详细信息 我试图在网站主页上显示所有用户的想法,但我正在努力为“想法”的作者显示正确的数据 其思想是获得用户思想的添加的(作者),然后使用持有添加的值的变量,并将其与用户表中的用户名进行比较 以下是我的表格及其字段: 用户想法: id message date_of_msg_

我正试图根据在另一个表中找到的id从一个表中获取数据。我的第一个表格叫做
user\u thinks
,这个表格保存了用户在我的“社交媒体网站”上发表的所有公开帖子的数据。我还有一个名为
users
的表,它存储了站点注册用户的所有详细信息

我试图在网站主页上显示所有
用户的想法
,但我正在努力为“想法”的作者显示正确的数据

其思想是获得
用户思想的
添加的
(作者),然后使用持有
添加的
值的变量,并将其与
用户
表中的
用户名进行比较

以下是我的表格及其字段:

用户想法

id
message
date_of_msg_post
time_of_msg_post
attachment
added_by
/* How it works: The id of each user_thought will be used to determine which user posted it. 
 * Then display their details accourdingly.
 */
$get_thoughts_from_db = mysqli_query($connect, "SELECT * FROM user_thoughts ORDER BY id DESC"); // newest posts first

while ($row = mysqli_fetch_assoc($get_thoughts_from_db)) {
    $thought_id      = $row['id'];
    $msg_content     = $row['message'];
    $date_of_msg     = $row['date_of_msg_post'];
    $thoughts_by     = $row['added_by'];
    $time_of_msg     = $row['time_of_msg_post'];
    $attachent       = $row['attachment'];
    $get_user        = $_GET['id'];
} // while closed   

    // Get the details of the user based on the ID of the user thought.
    $get_data = mysqli_query($connect, "SELECT * FROM users WHERE id = '$get_user'");                                   
    $get_user_data = mysqli_fetch_assoc($get_data);
        $author_fname = $get_user_data['first_name'];
        $user_profile_dp = $get_user_data['profile_pic'];

// displaying all the posts in the database on the main page.
// Will limit 15 posts per page, and will order them by the date and time posted (latest posts first)
$get_all_posts_q = mysqli_query ($connect, "SELECT * FROM user_thoughts ORDER BY id DESC ");
    $check_rows = mysqli_num_rows($get_all_posts_q);
    while ($get_row = mysqli_fetch_array($get_all_posts_q)){
        $message         = $get_row['message']; 

        /**** Between the while loop is where I echo the div(s)
                     which display the above details. *******/

}
用户
(仅显示相关字段)

Aim:从
user\u thinks
获取帖子的added\u by,然后使用added\u by从表
users
中的username字段获取added\u by的详细信息。注意:added_by和username将保留相同的值

以下是我尝试过的:

id
message
date_of_msg_post
time_of_msg_post
attachment
added_by
/* How it works: The id of each user_thought will be used to determine which user posted it. 
 * Then display their details accourdingly.
 */
$get_thoughts_from_db = mysqli_query($connect, "SELECT * FROM user_thoughts ORDER BY id DESC"); // newest posts first

while ($row = mysqli_fetch_assoc($get_thoughts_from_db)) {
    $thought_id      = $row['id'];
    $msg_content     = $row['message'];
    $date_of_msg     = $row['date_of_msg_post'];
    $thoughts_by     = $row['added_by'];
    $time_of_msg     = $row['time_of_msg_post'];
    $attachent       = $row['attachment'];
    $get_user        = $_GET['id'];
} // while closed   

    // Get the details of the user based on the ID of the user thought.
    $get_data = mysqli_query($connect, "SELECT * FROM users WHERE id = '$get_user'");                                   
    $get_user_data = mysqli_fetch_assoc($get_data);
        $author_fname = $get_user_data['first_name'];
        $user_profile_dp = $get_user_data['profile_pic'];

// displaying all the posts in the database on the main page.
// Will limit 15 posts per page, and will order them by the date and time posted (latest posts first)
$get_all_posts_q = mysqli_query ($connect, "SELECT * FROM user_thoughts ORDER BY id DESC ");
    $check_rows = mysqli_num_rows($get_all_posts_q);
    while ($get_row = mysqli_fetch_array($get_all_posts_q)){
        $message         = $get_row['message']; 

        /**** Between the while loop is where I echo the div(s)
                     which display the above details. *******/

}
当前行为:
目前,通过上面的代码,它显示了所有的
用户想法
,即表中有三行,每一行都在显示中,但是,其中的详细信息并不基于这些帖子的作者。例如,编写作者的用户的个人资料图片不显示

这链接了用户和用户的想法。记得清理你的输入

$get_thoughts_from_db = mysqli_query($connect, "SELECT * FROM user_thoughts INNER JOIN users ON user_thoughts.added_by = users.username ON ORDER BY id DESC"); // newest posts first

$query=您易受攻击。请注意,您在循环中获取用户的想法结果,但在循环完成之前,您不会使用这些结果,因此您只能使用上次获取的想法。您是否试图显示单个用户的想法列表,或者所有用户的想法列表?@OscarJ-用户想法表中的每一行都应显示在主页上。所以,为了回答您的问题,所有的用户。@MarcB-您是对的,但是使用问题中描述的while循环,所有的想法都被描述了?我之前确实遇到过一个问题,其中一个“想法”显示了三次(因为user_Thinks表中有三行),但是通过while循环,所有帖子现在都显示出来了。您真正应该做的是使用一个
JOIN
ed查询。每当您有嵌套查询,并且内部查询依赖于外部查询的数据时,它们可能/应该被重写为单个联接。