doforumda Posted February 9, 2010 Share Posted February 9, 2010 Hi i want to select data from db by comparing two things. First I want to select all the ids of currently logged in user's friends from friends table. then i want to select all the posts of currently logged in user's friends as well as his own posts from posts table. I am trying to achieve this task using following code but it does not work. <?php session_start(); $connect = mysql_connect("localhost","user","pass"); mysql_select_db("test"); $friends = mysql_query("SELECT * FROM friend WHERE userid='$_SESSION[userid]'"); while($friend = mysql_fetch_assoc($friends)) { $f = $friend['friendid']; //echo $f."<br />"; $query = mysql_query("SELECT * FROM posts WHERE userid='$f' AND userid='$_SESSION[userid]'"); while($id = mysql_fetch_assoc($query)) { $userpost = $id['post']; $date = $id['date']; echo $userpost."<br />".$date."<br />"; } } ?> Link to comment https://forums.phpfreaks.com/topic/191443-need-in-getting-data-from-db/ Share on other sites More sharing options...
doforumda Posted February 9, 2010 Author Share Posted February 9, 2010 ok here are my friend and post tables please help i am waiting for help [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/191443-need-in-getting-data-from-db/#findComment-1009243 Share on other sites More sharing options...
doforumda Posted February 9, 2010 Author Share Posted February 9, 2010 I am still waiting for help Link to comment https://forums.phpfreaks.com/topic/191443-need-in-getting-data-from-db/#findComment-1009269 Share on other sites More sharing options...
jl5501 Posted February 9, 2010 Share Posted February 9, 2010 It is possible that this could be done in one query, but, using the code you have, if you change the AND to OR in your second query then your results will probably be better. currently you are expecting the field userid to have 2 different values at the same time. Link to comment https://forums.phpfreaks.com/topic/191443-need-in-getting-data-from-db/#findComment-1009299 Share on other sites More sharing options...
doforumda Posted February 9, 2010 Author Share Posted February 9, 2010 It is possible that this could be done in one query, but, using the code you have, if you change the AND to OR in your second query then your results will probably be better. currently you are expecting the field userid to have 2 different values at the same time. i tried with OR as well but still have not the same result which i wanted Link to comment https://forums.phpfreaks.com/topic/191443-need-in-getting-data-from-db/#findComment-1009311 Share on other sites More sharing options...
doforumda Posted February 9, 2010 Author Share Posted February 9, 2010 It is possible that this could be done in one query, but, using the code you have, if you change the AND to OR in your second query then your results will probably be better. currently you are expecting the field userid to have 2 different values at the same time. i tried with OR as well but still have not the same result which i wanted now i use this code <?php $connect = mysql_connect("localhost","user","pass"); mysql_select_db("test"); $friends = "SELECT * FROM friend WHERE userid='$_SESSION[userid]'"; $result = mysql_query($friends); echo $friends."<br><br>"; while($friend = mysql_fetch_assoc($result)) { $f = $friend['friendid']; echo $f."<br />"; $query = "SELECT * FROM posts WHERE userid='$f' OR userid='$_SESSION[userid]'"; $result2 = mysql_query($query); echo $query."<br><br>"; while($id = mysql_fetch_assoc($result2)) { $userid = $id['post']; $date = $id['date']; echo $userid."<br />".$date."<br />"; } } ?> this is what it displays SELECT * FROM friend WHERE userid='1' 10 SELECT * FROM posts WHERE userid='10' OR userid='1' This is post of user 1 2010-02-02 This is second post of User 10 2010-02-01 11 SELECT * FROM posts WHERE userid='11' OR userid='1' This is post of user 1 2010-02-02 This is third post of user 11 2010-02-03 20 SELECT * FROM posts WHERE userid='20' OR userid='1' This is post of user 1 2010-02-02 This is last post of user 20 2010-02-05 Link to comment https://forums.phpfreaks.com/topic/191443-need-in-getting-data-from-db/#findComment-1009312 Share on other sites More sharing options...
jl5501 Posted February 9, 2010 Share Posted February 9, 2010 Yes ok the results are entirely what you would expect from what you are doing. each time you call the second query you will get the results of the logged in user as well as the particular friend. What you need to do, if using 2 queries, is to store all the ids you want plus the logged_in users id to , then query the posts against that list of ids Link to comment https://forums.phpfreaks.com/topic/191443-need-in-getting-data-from-db/#findComment-1009314 Share on other sites More sharing options...
jl5501 Posted February 9, 2010 Share Posted February 9, 2010 I would think that a one query solution would work better for you, so try select * from posts p,friend f where (p.userid=f.userid or p.userid=f.friendid) and f.userid='.$_SESSION['userid'] Link to comment https://forums.phpfreaks.com/topic/191443-need-in-getting-data-from-db/#findComment-1009318 Share on other sites More sharing options...
doforumda Posted February 9, 2010 Author Share Posted February 9, 2010 I would think that a one query solution would work better for you, so try select * from posts p,friend f where (p.userid=f.userid or p.userid=f.friendid) and f.userid='.$_SESSION['userid'] ok but i dont understand what are those p and f in this query? Link to comment https://forums.phpfreaks.com/topic/191443-need-in-getting-data-from-db/#findComment-1009320 Share on other sites More sharing options...
doforumda Posted February 9, 2010 Author Share Posted February 9, 2010 i am trying your query but i think i am putting it at wrong place can you please modify my abit? Link to comment https://forums.phpfreaks.com/topic/191443-need-in-getting-data-from-db/#findComment-1009329 Share on other sites More sharing options...
doforumda Posted February 9, 2010 Author Share Posted February 9, 2010 i am trying your query this way $friends = "select * from posts p,friend f where (p.userid=f.userid or p.userid=f.friendid) and f.userid='$_SESSION[userid]'"; $result = mysql_query($friends); echo $friends."<br><br>"; while($friend = mysql_fetch_assoc($result)) { $userid = $friend['post']; $date = $friend['date']; echo $userid."<br />".$date."<br />"; } it displays this select * from posts p,friend f where (p.userid=f.userid or p.userid=f.friendid) and f.userid='1' This is post of user 1 2010-02-02 This is second post of User 10 2010-02-01 This is post of user 1 2010-02-02 This is third post of user 11 2010-02-03 This is post of user 1 2010-02-02 This is last post of user 20 2010-02-05 Link to comment https://forums.phpfreaks.com/topic/191443-need-in-getting-data-from-db/#findComment-1009332 Share on other sites More sharing options...
doforumda Posted February 9, 2010 Author Share Posted February 9, 2010 my problem is still unsolved Link to comment https://forums.phpfreaks.com/topic/191443-need-in-getting-data-from-db/#findComment-1009367 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.