30

I have two databases, where I store who is following who and another which stores the posts the user makes.

I want to select all of the people a user is following from the following database and echo out the usernames of those who that user is following and query the posts database for posts of that user.

My problem is what if a user is following multiple users, I echoed out of the user id's of the people this user is following and I get 44443344330

When I separate each id with commans, I get:

44,44,33,44,33,0, 

so let's give that a variable of $user_ids;

$user_ids = "44,44,33,44,33,0, ";

the query:

$get_posts = mysql_query("SELECT * FROM posts WHERE userid = '$user_ids'");

but all it does is show the records of the first user id, 44.

How can I retrieve all of the records for all the users?

0

6 Answers 6

39

The query should be:

SELECT * FROM posts WHERE userid IN (44,44,33,44,33,0)

However, you may have to rethink your data model and make sure it is normalized, so that you can express this construction directly in the databse without echoing into a comma-separated string.

Why do you have two databases? Do you mean two tables?

Sign up to request clarification or add additional context in comments.

Comments

28

Maybe you want to use IN

SELECT * FROM posts WHERE userid IN ($user_ids)

1 Comment

if the data type for the id's is set to Integers this won't work. It needs to be ($user_ids) not ('$user_ids')
4

Given that you have two tables, not two databases, the easiest way to match multiple values for a specific column would be the following:

SELECT * FROM posts WHERE userid IN (44,44,33,44,33,0)

*A small point that I ran into when doing this. If you are matching to a column that is of type VARCHAR(n), be sure to wrap your matching values in 'single quotes', not "double quotes"

e.g:

SELECT * FROM posts WHERE name IN ('foo','bar','alpha','beta')

Comments

1

Assuming you have two tabels, not databases, and that the table (lets call it "friends") which describes who is following who is like

table friends(
  userid
  friendid
)

then query to get posts posted by X's friends would be

SELECT
 p.* 
FROM posts p
  JOIN friends f ON(p.userid = f.friendid)
WHERE f.userid = X

1 Comment

hey what if I replace the values ... it is possible that user id has the id of my friend and friend id has my id then what will you do ?? if a person sends me a friend request and I accept then the sender will be in user id and receiver will be friend id ... can u help ??
0
$get_posts = mysql_query("SELECT * FROM posts WHERE userid in '($user_ids)'");

Comments

0
$var= str_replace(","," or userid = ","userid =$your_data_from_db");

your myqsl_query = SELECT * FROM posts where $var

1 Comment

Is there a benefit to using the or userid = version of this instead of using just in?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.