What is the best practice for creating a friends list function in a PHP forum?

The best practice for creating a friends list function in a PHP forum is to have a separate table in the database to store the relationships between users who are friends. This table can have columns for the user ID of the user who initiated the friend request, the user ID of the user who accepted the friend request, and a status column to indicate the current status of the friendship (e.g., pending, accepted).

// Assuming you have a users table with user_id as the primary key
// Create a friends table to store friendships
CREATE TABLE friends (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id1 INT,
    user_id2 INT,
    status ENUM('pending', 'accepted') DEFAULT 'pending'
);

// Function to add a friend
function addFriend($user_id1, $user_id2) {
    $query = "INSERT INTO friends (user_id1, user_id2) VALUES ($user_id1, $user_id2)";
    // Execute the query to add the friendship
}

// Function to accept a friend request
function acceptFriendRequest($friendship_id) {
    $query = "UPDATE friends SET status = 'accepted' WHERE id = $friendship_id";
    // Execute the query to update the friendship status
}

// Function to get a user's friends
function getFriends($user_id) {
    $query = "SELECT * FROM friends WHERE (user_id1 = $user_id OR user_id2 = $user_id) AND status = 'accepted'";
    // Execute the query to retrieve the user's friends
}