What are the advantages of using usernames instead of IDs for retrieving data in PHP?

Using usernames instead of IDs for retrieving data in PHP can provide a more user-friendly and intuitive experience for users. It allows users to easily remember and reference their own username when accessing data, rather than needing to remember a specific ID number. Additionally, usernames can be more secure as they do not reveal any sensitive information about the user, unlike IDs which can potentially expose the internal structure of the database.

// Assuming we have a users table with columns 'id' and 'username'
$username = $_GET['username']; // Get the username from the request

// Retrieve user data using the username
$query = "SELECT * FROM users WHERE username = :username";
$stmt = $pdo->prepare($query);
$stmt->execute(['username' => $username]);
$user = $stmt->fetch();

// Use the retrieved user data
if($user) {
    // Display user information
    echo "Username: " . $user['username'] . "<br>";
    echo "Email: " . $user['email'] . "<br>";
    // Add more fields as needed
} else {
    echo "User not found";
}