我一直在讨论这个PHP代码很长一段时间。我正在尝试实现像
这样的东西 - >获取每个状态
- >让每个用户进入用户的好友列表
- >显示用户朋友列表中每个用户的状态
并重复直到没有更多。我一直在寻找一个解决方案,这几天真的很烦我。这是我试过的代码:
编辑:根据请求发布架构 https://kjf-tech.net/files/schema.png
<?php
$connect = new MySQLi($DBhost,$DBuser,$DBpass,$DBname);
$querya = "SELECT * FROM statuses ORDER BY `id` DESC";
$result = mysqli_query($connect, $querya);
$ALLDATA = array();
$DBcon2 = new MySQLi($DBhost,$DBuser,$DBpass,$DBname);
if ($DBcon2->connect_errno) {
die("ERROR : -> ".$DBcon2->connect_error);
}
while ($record = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
array_push($ALLDATA, $record);
$queryb = "SELECT * FROM friendslist WHERE idOfPerson1='".$record['idOfUser']."' OR idOfPerson2='".$record['idOfUser']."' OR idOfPerson2='".$userRow['user_id']."' OR idOfPerson1='".$userRow['user_id']."' ORDER BY `id` DESC";
$result2 = mysqli_query($connect, $queryb);
$ALLDATA2 = array();
while ($record2 = mysqli_fetch_array($result2, MYSQLI_ASSOC)) {
array_push($ALLDATA2, $record2);
if($record['idOfUser'] == $userRow['user_id']) {
echo '<div>You Posted on '.$record['whenPosted'].'<br />'.$record['content'].'</div>';
}
elseif($record2['idOfPerson1'] == $userRow['user_id']) {
$query2 = $DBcon2->query("SELECT * FROM tbl_users WHERE user_id='".$record2['idOfPerson2']."'");
$userRow2=$query2->fetch_array();
echo '<div>'.$userRow2['username'].' Posted on '.$record['whenPosted'].'<br />'.$record['content'].'</div>';
}
elseif($record2['idOfPerson2'] == $userRow['user_id']) {
$query2 = $DBcon2->query("SELECT * FROM tbl_users WHERE user_id='".$record2['idOfPerson1']."'");
$userRow2=$query2->fetch_array();
echo '<div>'.$userRow2['username'].' Posted on '.$record['whenPosted'].'<br />'.$record['content'].'</div>';
}
}
mysqli_free_result($result2);
}
$DBcon2->close();
mysqli_free_result($result);
?>
答案 0 :(得分:0)
您的架构看起来不错,但让我们再看一下这些关系。为方便起见,我将重命名一些列。
Users:
+- user_id
| user_name
|
|
| Friendships:
| fid
+- user_id
| friend_id --------+
| friendship_start |
| |
| |
| Statuses: |
| sid |
+- user_id ----------+
post_date
content
如果您只是想查找朋友的状态,那么查询将如此:
SELECT statuses.content
FROM friendships, statuses
WHERE friendship.user_id=? AND
friendships.friend_id = statuses.user_id
当您bind
声明时,您当然会user_id
->prepare()
值。
(请参阅http://php.net/manual/en/mysqli.prepare.php了解正确的sql方法。过去想要做类似mysql_query的事情(&#34; select * from table where id = &#34;。$ _ POST [&#39; id&#39;])因为它对SQL注入开放了)
不幸的是,这不包括您在查询结果中的状态。我们将不得不在查询上做更多的工作......
SELECT statuses.content
FROM friendships, statuses
WHERE
( friends.user_id = ? AND
friends.friend_id = stuatuses.user_id )
OR
statuses.user_id = ?
ORDER BY statuses.post_date DESC
到目前为止,这么好......但我们没有姓名或发布日期。发布日期很简单,只需将其添加到选择中:
SELECT statuses.content, statuses.post_date
要添加名称,我们还必须从用户处获取数据。
SELECT users.user_name, statuses.content, statuses.post_date
FROM users, friendships, statuses
WHERE
users.user_id = ? AND
(
( users.user_id = friendships.user_id AND
friendships.friend_id = statuses.user_id )
OR
statuses.user_id = users.user_id
)
ORDER BY statuses.post_date DESC
你有它;数据库为您完成所有工作。不需要嵌套查询等。这只会为您提供在页面上打印的简单列表。请记住,这不在我的头脑中,所以如果我忽略了某些东西,你可能不得不调整它。