What is the best practice for creating individual user profile pages in PHP?

When creating individual user profile pages in PHP, it is best practice to use a unique identifier (such as a user ID) to fetch the user's information from a database and display it on the profile page. This helps ensure that each user's profile page is dynamic and personalized.

<?php
// Assuming $userId contains the unique identifier of the user
// Fetch user information from the database based on $userId

// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Query to fetch user information
$sql = "SELECT * FROM users WHERE id = $userId";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Name: " . $row["name"]. "<br>";
        echo "Email: " . $row["email"]. "<br>";
        // Add more fields as needed
    }
} else {
    echo "User not found";
}

$conn->close();
?>