Jump to content

mysql query to compare & display


php_begins

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/238599-mysql-query-to-compare-display/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.