jakebur01 Posted March 9, 2009 Share Posted March 9, 2009 My table for displaying members has been working ok when querying all members. I have been trying to add an extra loop that will make the table only display members that are friends of a specific member. My problem is that it is grabbing the same member twice. For example if Fred is UserId1 and Tom is UserId2, then row 2 Joe is UserId1 and Fred is UserId2. It will display Fred twice. Also, I cannot get my <tr>'s to work correctly. echo '<table width="100%" border="0">'; $getimages = mysql_query("SELECT UserId1, UserId2 FROM life_approval WHERE (`UserId2`='$q' OR `UserId1`='$q') AND `Approved` = 'Approved'", $db); if(!$getimages) die("Cannot execute query. " . mysql_error()); $countRows = mysql_num_rows($getimages); $i = 0; if ($countRows > 0) { while ($rowimages = mysql_fetch_assoc($getimages)) { $result = mysql_query("SELECT Username, Company, Country, State, Link FROM life_useraccount WHERE (`Username`='$rowimages[userId1]' OR `Username`='$rowimages[userId2]') ", $db) or die(mysql_error()); if ($i % 3 == 0) echo ($i > 0? '</tr>' : '') . '<tr>'; while ($myrow = mysql_fetch_array($result)){ //$id=$myrow["Id"]; echo '<td><center><div class="product"><a href="profile.php?q='.$myrow['Username'].'"><img src="'.$myrow['Link'].'" width="145" border="0" /></a></div><b>'.$myrow["Company"].'</b><br />'.$myrow["State"].'<br />'.$myrow["Country"].'</center></td>'; } if ($i == $countRows - 1) echo '</tr>'; $i++; } } echo '</table>'; Quote Link to comment https://forums.phpfreaks.com/topic/148558-solved-trouble-with-query/ Share on other sites More sharing options...
jakebur01 Posted March 9, 2009 Author Share Posted March 9, 2009 I tried using DISTINCT, but that did not do the trick. SELECT DISTINCT UserId1, UserId2 FROM life_approval WHERE (`UserId2`='$q' OR `UserId1`='$q') AND `Approved` = 'Approved' Quote Link to comment https://forums.phpfreaks.com/topic/148558-solved-trouble-with-query/#findComment-780111 Share on other sites More sharing options...
fenway Posted March 9, 2009 Share Posted March 9, 2009 Why not just JOIN them? Quote Link to comment https://forums.phpfreaks.com/topic/148558-solved-trouble-with-query/#findComment-780151 Share on other sites More sharing options...
kickstart Posted March 9, 2009 Share Posted March 9, 2009 Hi Not quite sure what you are trying to do. Can you explain exactly the database structure and what you are trying to get. Seems strange to have 2 userids on one table. Unless the first table is a list of all the friends (ie, UserId1 would be Fred, while there are then multiple rows each with a different UserId2). All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/148558-solved-trouble-with-query/#findComment-780190 Share on other sites More sharing options...
jakebur01 Posted March 9, 2009 Author Share Posted March 9, 2009 Hi, I am trying to query the friend table (life_approval)columns(Id, UserId1, UserId2, and Approve) and see who all is a friend of the variable $q. I am trying to do this by checking both columns, if $q is a friend to someone..... he/she could be in either column. When a friend invite is stored in this table, the person the invite is sent to is stored in UserId1. {which my attempt appears to be pulling everyone out of the table in columns UserId1 and UserId2} The second query is supposed to be collecting information on the friend. I was trying to do that by using ( WHERE `Username` = 'UserId1 from the first query OR `Username` = 'UserId2' from the first query. Thanks, Jake Quote Link to comment https://forums.phpfreaks.com/topic/148558-solved-trouble-with-query/#findComment-780274 Share on other sites More sharing options...
kickstart Posted March 9, 2009 Share Posted March 9, 2009 Hi Think you can do that in one piece of SQL SELECT Username, Company, Country, State, Link FROM life_useraccount WHERE Username IN (SELECT UserId1 as UserId FROM life_approval WHERE `UserId2`='$q' UNION SELECT UserId2 as UserId FROM life_approval WHERE `UserId1`='$q' ) (not tested). All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/148558-solved-trouble-with-query/#findComment-780278 Share on other sites More sharing options...
jakebur01 Posted March 9, 2009 Author Share Posted March 9, 2009 Man, you are amazing. It works perfect. Thank you. Where are some good places to learn more about MySQL queries? Quote Link to comment https://forums.phpfreaks.com/topic/148558-solved-trouble-with-query/#findComment-780281 Share on other sites More sharing options...
kickstart Posted March 9, 2009 Share Posted March 9, 2009 Hi Not sure to be honest. With me more a case of having a fair grounding in SQL (been using it since 1990 with DB2), and then just trying to think of alternative ways of doing things. Sometimes you land up barking up the wrong tree or find someone had a far better idea. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/148558-solved-trouble-with-query/#findComment-780283 Share on other sites More sharing options...
jakebur01 Posted March 9, 2009 Author Share Posted March 9, 2009 Thanks Keith. Quote Link to comment https://forums.phpfreaks.com/topic/148558-solved-trouble-with-query/#findComment-780285 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.