Current Location: Home> Latest Articles> Complete Guide to Developing a Mutual Follow Feature with PHP for Social Platforms

Complete Guide to Developing a Mutual Follow Feature with PHP for Social Platforms

M66 2025-07-02

Requirements Analysis of the Mutual Follow Feature

With the widespread popularity of social networks, mutual following has become an essential way to connect users. By following each other, users can receive real-time updates on their contacts' activities and interact more effectively. Before developing the mutual follow feature, it is important to clarify the core requirements:

  • Users can follow other users to conveniently view their updates.
  • Users can view their own follow lists and have the option to unfollow.
  • Users can view the list of fans who follow them.
  • The homepage shows the latest updates from followed users.

Database Design

To support the mutual follow feature, the database structure needs to be designed accordingly:

  • User table (user): contains user ID (user_id), username (username), password (password), and other basic information.
  • Follow relation table (following): contains follower ID (follower_id), followed user ID (followed_id), follow timestamp (follow_time), etc.

Implementation Details

User Follow Operation

$follower_id = $_GET['follower_id'];  // Get follower ID
$followed_id = $_GET['followed_id'];  // Get followed user ID
$follow_time = time();  // Get current timestamp

$sql = "INSERT INTO following (follower_id, followed_id, follow_time) VALUES ($follower_id, $followed_id, $follow_time)";
$result = mysqli_query($conn, $sql);

Querying Follow and Fan Lists

$user_id = $_GET['user_id'];  // Get user ID

// Query follow list
$sql1 = "SELECT * FROM following WHERE follower_id = $user_id";
$result1 = mysqli_query($conn, $sql1);

// Query fan list
$sql2 = "SELECT * FROM following WHERE followed_id = $user_id";
$result2 = mysqli_query($conn, $sql2);

// Output follow list
while ($row1 = mysqli_fetch_assoc($result1)) {
    $followed_id = $row1['followed_id'];
    $followed_user = mysqli_query($conn, "SELECT * FROM user WHERE user_id = $followed_id");
    $row = mysqli_fetch_assoc($followed_user);
    echo $row['username'];  // Output followed user's name
}

// Output fan list
while ($row2 = mysqli_fetch_assoc($result2)) {
    $follower_id = $row2['follower_id'];
    $follower_user = mysqli_query($conn, "SELECT * FROM user WHERE user_id = $follower_id");
    $row = mysqli_fetch_assoc($follower_user);
    echo $row['username'];  // Output fan's name
}

Displaying Dynamic Content

$user_id = $_GET['user_id'];  // Get user ID

// Query follow list
$sql = "SELECT * FROM following WHERE follower_id = $user_id";
$result = mysqli_query($conn, $sql);

// Output dynamic updates
while ($row = mysqli_fetch_assoc($result)) {
    $followed_id = $row['followed_id'];
    $dynamic = mysqli_query($conn, "SELECT * FROM dynamic WHERE user_id = $followed_id ORDER BY dynamic_time DESC LIMIT 10");
    while ($row = mysqli_fetch_assoc($dynamic)) {
        echo $row['content'];  // Output dynamic content
    }
}

Summary

The core of implementing a mutual follow feature lies in CRUD operations on the database. By designing a proper database schema and writing corresponding PHP code, user follow relationships and dynamic content display can be efficiently managed. Developers can further enhance this functionality by adding notifications, grouping follows, and more. The sample code provided here offers a clear approach to quickly integrating this feature into your projects.