加载每个朋友的帖子

时间:2013-12-21 19:00:08

标签: php mysql

我目前正在处理“新闻Feed”类型的脚本,我正在尝试加载用户朋友创建的帖子。我目前的代码部分有效;它只从一个朋友加载并完成。我做错了什么?

<?php

$infofriends = mysql_fetch_array(mysql_query("SELECT * FROM btfriend WHERE `user_id`='".$infousr['auto']."'"));
$infofrnd = mysql_fetch_array(mysql_query("SELECT * FROM btaccs WHERE `auto`='".$infofriends['friend_id']."'"));
$posts = mysql_query("SELECT * FROM btpost WHERE `user`='".$infofrnd['user']."' ORDER BY `auto` DESC") or die('Error: '.mysql_error());


while($row = mysql_fetch_array( $posts )) {
$infobeer = mysql_fetch_array(mysql_query("SELECT * FROM btbeer WHERE `beer`='".$row['beer']."'"));
$infouser = mysql_fetch_array(mysql_query("SELECT * FROM btaccs WHERE `user`='".$row['user']."'"));
....

(目前很草率,之后我会对其进行编辑)。

MySQL结构:

btfriend

mysql> DESCRIBE btfriend;
+-----------+---------+------+-----+---------+----------------+
| Field     | Type    | Null | Key | Default | Extra          |
+-----------+---------+------+-----+---------+----------------+
| auto      | int(11) | NO   | PRI | NULL    | auto_increment |
| user_id   | int(11) | YES  |     | NULL    |                |
| friend_id | int(11) | YES  |     | NULL    |                |
+-----------+---------+------+-----+---------+----------------+

btaccs

mysql> DESCRIBE btaccs;
+-----------+--------------+------+-----+---------+----------------+
| Field     | Type         | Null | Key | Default | Extra          |
+-----------+--------------+------+-----+---------+----------------+
| auto      | int(11)      | NO   | PRI | NULL    | auto_increment |
| user      | varchar(150) | YES  |     | NULL    |                |
| display   | varchar(150) | YES  |     | NULL    |                |
| pass      | varchar(250) | YES  |     | NULL    |                |
| email     | varchar(150) | YES  |     | NULL    |                |
| firstname | varchar(150) | YES  |     | NULL    |                |
| lastname  | varchar(150) | YES  |     | NULL    |                |
+-----------+--------------+------+-----+---------+----------------+

btpost

mysql> DESCRIBE btpost;
+---------+---------------+------+-----+---------+----------------+
| Field   | Type          | Null | Key | Default | Extra          |
+---------+---------------+------+-----+---------+----------------+
| auto    | int(11)       | NO   | PRI | NULL    | auto_increment |
| user    | varchar(500)  | YES  |     | NULL    |                |
| beer    | varchar(911)  | YES  |     | NULL    |                |
| img     | varchar(30)   | YES  |     | NULL    |                |
| rate    | varchar(10)   | YES  |     | NULL    |                |
| loc     | varchar(1000) | YES  |     | NULL    |                |
| comment | varchar(1500) | YES  |     | NULL    |                |
| fb      | varchar(10)   | YES  |     | NULL    |                |
| type    | int(2)        | YES  |     | NULL    |                |
+---------+---------------+------+-----+---------+----------------+

如果有人可以提供帮助,那就太棒了!

3 个答案:

答案 0 :(得分:1)

首先,你正在使用mysql_ *函数;那些已被弃用,并将在未来的某个时候停止工作。看看切换到mysqli_或PDO - 它们都可以更容易编写更安全的代码。

其次,您正在调用您的代码,以期让它起作用:

$infofriends = mysql_fetch_array(mysql_query("SELECT * FROM btfriend WHERE `user_id`='".$infousr['auto']."'"));

如果查询失败,您将传递一个布尔值false给mysql_fetch_array;一次制作一个语句要容易得多,并在出现错误时处理错误:

$friendset = mysql_query("SELECT * FROM btfriend WHERE `user_id`='".$infousr['auto']."'") or die (mysql_error());
$infofriends = mysql_fetch_array($friendset) or die (mysql_error());

如果出现问题,这将在适当的行上生成错误 - 这是一些代码,但调试和维护更容易。

最后,你的实际问题;你只收到一个朋友,因为你只在朋友查询时调用mysql_fetch_array()一次;这将返回到第一行。一个快速的解决方案是分别遍历结果以生成列表,然后将其传递给第二个查询:

$friendset = mysql_query("SELECT * FROM btfriend WHERE `user_id`='".$infousr['auto']."'") or die (mysql_error());

$friendArray = array();

while ($infofriends = mysql_fetch_array($friendset)) {
    $friendArray[] = $infofriends['friend_id'];
}
$friendArray[] = $infousr['auto'];

// At this point, you have an array of friend IDs.

$posts = mysql_query("SELECT * FROM btpost INNER JOIN btaccs ON btpost.user=btaccs.user WHERE btaccs.auto IN (" . implode(',', $friendArray) . ") ORDER BY btpost.auto DESC") or die('Error: '.mysql_error());

请注意,它使用IN一次检索所有ID;你可能想在ORDER BY中添加一个额外的参数来安排朋友的帖子。

请注意,我没有对此进行测试,因此语法可能存在问题,但我希望您能够获得一般性的想法。

- 我们在聊天中解决了这个问题并添加了$friendArray[] = $infousr['auto'];,其中还包括当前用户的帖子。然后,当前用户的ID以及朋友ID将进入查询中的implode函数。我们还链接了btpostbtaccs表,因为btaccs拥有用户的ID,而btpost持有用户名。 Full chat transcript -alexpja

答案 1 :(得分:1)

使用带连接的单一查询尝试此操作

SELECT p.* 
FROM btpost p
INNER JOIN btfriend f ON (p.`user` = f.friend_id)
WHERE f.user_id  =$infousr['auto']

然后遍历查询中的所有结果,这将为您提供btpost的用户等于朋友的身份btfriend的所有帖子,这些是您指定用户ID的朋友{ {1}}我认为$infousr['auto']将拥有用户ID

答案 2 :(得分:0)

我认为你错过了一个循环。让我们分解您的代码:

<?php

// Here, you run a query that presumably returns multiple rows, but you're only looking at the first row:
$infofriends = mysql_fetch_array(mysql_query("SELECT * FROM btfriend WHERE `user_id`='".$infousr['auto']."'"));
// That gave you a single array of the first friend that MySQL found

// Now, you take the `friend_id` field from that single result, and you run it against the `btaccs` table to get some more information:
$infofrnd = mysql_fetch_array(mysql_query("SELECT * FROM btaccs WHERE `auto`='".$infofriends['friend_id']."'"));
// Once again, you have a single row at this point. (Although here, I'm assuming that's ok, since a user's ID probably appears only once in this table

// Now, you take the single user that you've looked up, and you find posts associated with that user:
$posts = mysql_query("SELECT * FROM btpost WHERE `user`='".$infofrnd['user']."' ORDER BY `auto` DESC") or die('Error: '.mysql_error());

我认为您可以看到它是第一个将其限制为单个用户的语句。尝试在运行该语句之前创建一个数组,然后循环遍历结果集,添加到该数组。

我认真考虑的其他一些事情:

1)利用PHP的双引号字符串替换......

mysql_query("SELECT * FROM btfriend WHERE `user_id`='{$infousr['auto']}'"

更容易阅读
mysql_query("SELECT * FROM btfriend WHERE `user_id`='".$infousr['auto']."'"

2)阅读MySQL中的JOIN。您在此处所做的一切实际上都可以折叠成MySQL中的单个查询。这里详细介绍一下,但是你可以从MySQL文档开始:http://dev.mysql.com/doc/refman/5.0/en/join.html