【发布时间】:2017-01-27 05:16:54
【问题描述】:
我需要一些有关我的 mysql 查询代码的帮助!我有三个数据库,我正在尝试找到一种方法来发布所有三个数据库中的数据。
users
----------------------------------
user_id username email..etc
-----------------------------------
1 username
-----------------------------------
friends_db
-------------------------------------
user_id friend_id friend_active
--------------------------------------
1 2 1
--------------------------------------
posts
-------------------------------------
post_id user_id post_content
-------------------------------------
1 1 this is a post
--------------------------------------
我想做的是创建一个朋友帖子列表,您可以在其中查看您的朋友(在friend_active 中为 1 的人)所做的所有帖子。
我的功能:
function getFriendsPosts()
{
require "config.php";
$friends = $c->query ( "SELECT * FROM friends_db WHERE user_id=" . $_SESSION["user_id"] ." AND friend_active = 1" ) ;
if ( $friends->num_rows > 0 )
{
while( $row = $friends->fetch_assoc() )
{
$postData[] = array( "user_id" => $row[ "user_id" ], "friend_active" => $row[ "friend_active" ], "friend_id" => $row[ "friend_id" ] );
}
} else {
echo "No Data To Show";
}
return $postData;
}
我有:
$friends = $c->query ( "SELECT * FROM friends_db WHERE user_id=" . $_SESSION["user_id"] ." AND friend_active = 1" ) ;
这给了我有friend_active = 1的任何人的user_id
但是我不知道如何通过将friend_id 与他们自己的user_id 相关联来扩展此功能,以获取他们的用户名,然后他们的帖子也将在其中显示。
我可能过于复杂了,或者我可能不会,我不知道。 有谁知道我如何以我已经完成的相同格式执行此操作(我还有其他事情正在处理这种格式,我也需要这样做)?或者有什么建议?
编辑:
$friends = getFriendsPosts();
foreach ($friends as $friend) {
?>
<div class="ui-corner-all custom-corners">
<div id="searchpost">
<?php
echo $friend['user_id'];
echo $friend['friend_id'];
echo $friend['friend_active'];
?>
</div>
</div>
<?php
}
?>
我喜欢:
echo 用户名(与friend_id/他们自己的用户ID 相关) 回显帖子数据(与 user_id 相关)
【问题讨论】: