Jump to content

Query Results In Another Query


slj90

Recommended Posts

I have a table 'friends' which has username and friend columns so if a user is friends with another user it is saved in this table. I also have another table with posts users have made. I am trying to make it only show posts from the users friends. However it is only running the query for the first 'friend' in the table. What's wrong?

 

 

<?php


$query = "SELECT * FROM friends WHERE username='$username'";
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result)) {


$friend = $row['follow'];



$query = "SELECT * FROM posts WHERE username='$friend'";
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result)) {


$id=$row['id'];
$post=$row['post'];


echo $post;
echo " posted by ";
echo $friend;
echo "<br><br><br>";


}
}

?>

Link to comment
https://forums.phpfreaks.com/topic/270625-query-results-in-another-query/
Share on other sites

use a single query with a JOIN

 

SELECT *
FROM friends f
INNER JOIN posts p ON p.username=f.follow
WHERE f.username='$username'

 

I don't quite understand how to do this...

 

I just read a tutorial on it and came up with this:

 

 

 

$query = "SELECT posts.post, friends.username,  ".
"FROM friends, posts ".
"WHERE posts.username = friends.friend";

 

I'm confused, where does it get the usernames friends from?

I think Barand has missed something in his post.. I think query should something similar to this..

 

SELECT * FROM friends AS f 
INNER JOIN posts AS p ON p.username = f.follow
WHERE f.username = '$username'

 

This is not suit for you.. explain your 2 tables with columns...

Table one 'Posts' has the columns 'Username' and 'Post'.

Table two 'Friends' has the columns 'Username' and 'Friend' (friends username).

 

If those are your column names, where did this come from

 

$friend = $row['follow'];

I think Barand has missed something in his post.. I think query should something similar to this..

 

SELECT * FROM friends AS f
INNER JOIN posts AS p ON p.username = f.follow
WHERE f.username = '$username'

 

This is not suit for you.. explain your 2 tables with columns...

 

@thara If you ever get around to reading the MySQL manual you will see the the AS is optional, denoted by [ .. ]

For each table specified, you can optionally specify an alias.

tbl_name [[AS] alias]

 

Can you please explain why a JOIN is not suitable here?

yes what does mean

 

$friend = $row['follow'];

 

why you use 'username' column in both table?

 

$row['follow'] should be $row['friend']

 

Username is used in the post table to save who posted it.

Username is used in the friends table so when a user is friends with someone it creates a row with their Username and their friends username.

 

Thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.