Search code examples
phpmysqlprepared-statementfeed

Is there a way to identify which data has been selected with the sql statement


I have a sql statement:

$feed=$conn->prepare("SELECT * FROM posts WHERE post_by=? OR id=? ORDER BY id DESC LIMIT 10");
$feed->bind_param("ii",$friend['id'],$like[id]);
$feed->execute();

The $friend['id'] is the id of a user's friend, $like[id] is the id of a like by the user's friend.

The posts fetched with this query appear in a page.

What I want is I want to know which all posts have been posted by the user's friends (Which all posts have been fetched using $friends['id']) and which all posts have been liked by the user's friends and appear in the feed(Which all posts have been fetched using $like['id'])

I want to know all possibilities I can try to achieve what I want.

I have tried varying my query with UNION ALL but it shows errors and I could'nt achieve what I want.

Currently there are no errors but I want the user to know how this post appeared in the newsfeed.

Hope you all get a good idea about my question and all types of hacks are also accepted as I want in someway to achieve the result I would also agree to change mt query nature.

Please comment for more info.

Thanks in advance.


Solution

  • SELECT *, post_by = ?postId AS post_by_friend
    FROM posts
    WHERE post_by = ?postId OR
          id = ?friendId
    ORDER BY id DESC
    LIMIT 10
    

    post_by_friend will be 1 if it matched the first condition, otherwise 0. I haven't benchmarked it, but this method should be faster than StuartLC's UNION suggestion.