php_begins Posted June 6, 2011 Share Posted June 6, 2011 I was working on a php based application where a user has followers and follows(Similar to twitter) I need help in running a query. $profileuserid has the the userid of the current profile. Table follow has following fields : followerid & followid Table users has following fields: userid & username I need to run a php mysql query that retrieves the usernames from the user tables with the logic below: I need help with the syntax and execution 1. "SELECT followerid FROM follow WHERE followid='$profileuserid'"; 2. Select username from userstable where userid="followid"; i.e.I need to display all the associated usernames. How do I go about it and display it all at once? Quote Link to comment https://forums.phpfreaks.com/topic/238599-mysql-query-to-compare-display/ Share on other sites More sharing options...
jcbones Posted June 6, 2011 Share Posted June 6, 2011 include 'db.config.php'; $sql = "SELECT u.username FROM follow AS f JOIN users AS u ON f.follower = u.userid WHERE f.followid = '$profileuserid'"; $result = mysql_query($sql) or trigger_error($sql . ' has an error.<br />' . mysql_error()); if(mysql_num_rows($result) > 0) { while($row = mysql_fetch_row($result)) { echo $row[0] . '<br />'; } } else { echo 'No followers'; } UN-Tested. Quote Link to comment https://forums.phpfreaks.com/topic/238599-mysql-query-to-compare-display/#findComment-1226151 Share on other sites More sharing options...
php_begins Posted June 7, 2011 Author Share Posted June 7, 2011 Thank you so much jcbones! Works great! Quote Link to comment https://forums.phpfreaks.com/topic/238599-mysql-query-to-compare-display/#findComment-1226202 Share on other sites More sharing options...
jcbones Posted June 7, 2011 Share Posted June 7, 2011 Thank you so much jcbones! Works great! You're welcome! Quote Link to comment https://forums.phpfreaks.com/topic/238599-mysql-query-to-compare-display/#findComment-1226223 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.