I'm trying to return & display all of the user profile images from the DB, but only those whom are friends of the person in that particular profile. The DB is structure like this:
The friend_array is separated by ,
I have the following for access and display:
<?php
global $con;
$username = $_GET['profile_username'];
$query = mysqli_query($con,"SELECT * FROM users WHERE profile_pic = '$username'");
while ($row = mysqli_fetch_array($query)) {
$friends = $row['profile_pic'];
?>
<div><img src="<?php echo $friends; ?>"></div>
<?php } ?>
I'm certain it's the query, however any help on how to better make this query would be awesome.
Thankyou.
You would need something like this:
$query = mysqli_query($con,"SELECT * FROM users WHERE friend_array LIKE '$username,%' OR friend_array LIKE '%,$username,%' OR friend_array LIKE '%,$username'");
You are looking for people who are friends of $username
, so you need to be checking against friend_array. I used LIKE because it gives us easier searching against a comma-separated field (a straight =
won't work for partial matches). We have to check 3 separate possible ways of matching because a username may be the first username, may be in the middle of the list, or may be the last, and each of the three have a different format.