How can database queries be optimized to handle multiple referrals in a PHP system efficiently?

To optimize database queries for handling multiple referrals efficiently in a PHP system, you can use the IN clause to fetch data for multiple referral IDs in a single query instead of making individual queries for each referral. This reduces the number of database calls and improves performance.

// Assume $referralIds is an array of referral IDs
$referralIds = [1, 2, 3, 4, 5];

// Construct a comma-separated string of referral IDs
$referralIdsString = implode(',', $referralIds);

// Query to fetch data for multiple referrals using the IN clause
$query = "SELECT * FROM referrals WHERE id IN ($referralIdsString)";

// Execute the query and fetch the results
$result = mysqli_query($conn, $query);

// Process the results as needed
while ($row = mysqli_fetch_assoc($result)) {
    // Process each row of data
}