Member Avatar for Member #743307

Hi everyone

I am trying to develop a friend system where you can view friends who you are friends with.
I have been able to get it to show one result but it wont show any other. Please can someone tell me what I am doing wrong.
Here is the code

<?php
require("check_session.php");

$id = $_SESSION['id'];
$username = $_SESSION['username'];

?>
Here is a list of your current friends.
<br />
<br />
<?php

$sql = mysql_query("SELECT * FROM friends WHERE user_id='$id' AND friend='1'");
while($fetch = mysql_fetch_assoc($sql))
{

$friend_id = $fetch['friend_id'];

}
?>
<?php

$usertable = mysql_query("SELECT * FROM users WHERE id='$friend_id'");

while($row = mysql_fetch_assoc($usertable))
{
$table_first = $row['firstname'];
$table_last = $row['lastname'];

echo $table_first; echo" "; echo $table_last; echo"<br /><br />";	
}
?>

Thanks in advance

Dani AI

Generated

Brief summary and cause: the friends query in the original post collects multiple rows but ends up using a single scalar friend_id, so only the last record is shown. correctly suggested aggregating IDs and using an IN-style lookup; correctly pointed out the need to check the query result count before continuing. The approach below shows a cleaner, safer pattern that avoids building ID lists by hand and scales better.

A single, parameterized query that joins the friends table to users returns all friend rows in one pass. Example (PDO, safe output and basic empty-check):

// $pdo is a configured PDO instance
$stmt = $pdo->prepare(
  "SELECT u.firstname, u.lastname
   FROM friends f
   JOIN users u ON u.id = f.friend_id
   WHERE f.user_id = :uid AND f.friend = 1
   ORDER BY u.lastname, u.firstname"
);
$stmt->execute([':uid' => $id]);
$friends = $stmt->fetchAll(PDO::FETCH_ASSOC);

if (count($friends) === 0) {
    echo "No friends found.";
} else {
    foreach ($friends as $f) {
        echo htmlspecialchars($f['firstname'], ENT_QUOTES) . ' '
           . htmlspecialchars($f['lastname'], ENT_QUOTES) . "<br><br>";
    }
}

Notes and troubleshooting tips: set PDO to throw exceptions (ERRMODEEXCEPTION) and wrap calls in try/catch to surface SQL errors instead of silent failures. Avoid die() for user-facing messages — return a user message or render a placeholder. If sticking with older `mysqlcode, always test the query result (not the SQL string) before callingmysqlfetchand usemysql_num_rows()` on the result resource.

Extra recommendations: add indices on friends(user_id) and friends(friend_id), add pagination with LIMIT/OFFSET for large lists, and model friend state (pending/blocked) so the WHERE clause filters correctly.

Recommended Answers

All 5 Replies

Hi, you doing some wrong things.
- here you get from DB all user frend, but in loop you use a one variable which will overwrite it value, you have to use a array to put in all geted values... you have to do this :

$sql = mysql_query("SELECT * FROM friends WHERE user_id='$id' AND friend='1'");
while($fetch = mysql_fetch_assoc($sql)) {
   $friend_id[] = $fetch['friend_id'];
}

now in $friend_id[] you have all friend_ids.
Now you have to to do this :

$frends = '('.implode(', ', $friend_id).')';
$usertable = mysql_query("SELECT * FROM users WHERE id IN $frends ");
Member Avatar for Member #743307

Thank you so much storm 123. Your help is really appreciated!

Member Avatar for Member #743307

Hi, you doing some wrong things.
- here you get from DB all user frend, but in loop you use a one variable which will overwrite it value, you have to use a array to put in all geted values... you have to do this :

$sql = mysql_query("SELECT * FROM friends WHERE user_id='$id' AND friend='1'");
while($fetch = mysql_fetch_assoc($sql)) {
   $friend_id[] = $fetch['friend_id'];
}

now in $friend_id[] you have all friend_ids.
Now you have to to do this :

$frends = '('.implode(', ', $friend_id).')';
$usertable = mysql_query("SELECT * FROM users WHERE id IN $frends ");

Is there a way to echo (die) a message to the user if they have no friends on their account because when I use 'mysql_num_rows' I get a message saying

Warning: implode() [function.implode]: Invalid arguments passed in /home/myusername/public_html/friends.php on line 30

PHP Error Message

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/Red/public_html/friends.php on line 33

This is what I added

$numrows = $sql;
if($numrows==0)
{
die ("You currently do not have any friends on photoshare");
}
else

Do you know what I should do?

Thanks in advance

$numrows = mysql_num_rows($sql);
if($numrows==0)
{
die ("You currently do not have any friends on photoshare");
}
Member Avatar for Member #743307
$numrows = mysql_num_rows($sql);
if($numrows==0)
{
die ("You currently do not have any friends on photoshare");
}

Thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.