如何仅显示特定用户的记录?

时间:2016-10-13 06:01:05

标签: php mysqli

我创建了两个表用户,其中 user_id为主键 post_id为帖子表的主键

我可以显示所有可用用户的所有帖子,

但是当我尝试根据特定的user_id显示所有帖子时,它仍会显示所有用户的所有帖子。我想要的是仅来自特定user_id的所有帖子。有人可以帮忙吗?感谢

<?php
    include("includes/connections.php");

    function display_post_from_specific_profile(){

        global $connect;
        global $user_id;

            $get_post = "SELECT * FROM posts";
            $run_post = mysqli_query($connect, $get_post);
            while($row_post = mysqli_fetch_array($run_post)){
                $post_id         = $row_post['post_id'];
                $user_id         = $row_post['user_id'];
                $post_title      = $row_post['post_title'];

                    $get_user    =  "SELECT * FROM users WHERE user_id='$user_id' AND post='yes'";
                    $run_user    = mysqli_query($connect, $get_user);
                    $check_user  = mysqli_fetch_array($run_user);

                            $user_name  = $check_user['name'];
                            echo    "$post_title $user_name";
            }
    }
?>

1 个答案:

答案 0 :(得分:0)

你应该使用

$get_user    =  "SELECT * FROM users WHERE user_id='$user_id'

取代

$get_user    =  "SELECT * FROM users WHERE user_id='$user_id' AND post='yes'";

它会起作用。