What are some best practices for querying databases in PHP to display friend list data?

When querying databases in PHP to display friend list data, it is best to use prepared statements to prevent SQL injection attacks. Additionally, it is recommended to only select the necessary columns and rows to optimize performance. Lastly, consider implementing pagination to display the friend list data in manageable chunks.

<?php
// Establish a database connection
$pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');

// Prepare a query to select friend list data
$stmt = $pdo->prepare("SELECT friend_id, friend_name FROM friends WHERE user_id = :user_id");

// Bind the user_id parameter
$user_id = 1; // Assuming user_id is 1
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);

// Execute the query
$stmt->execute();

// Fetch and display friend list data
while ($row = $stmt->fetch()) {
    echo $row['friend_id'] . ' - ' . $row['friend_name'] . '<br>';
}
?>