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();
?>
Related Questions
- What is the best practice for filtering database query results based on a specific status value in PHP?
- What are the potential pitfalls of trying to use a LIKE operator in an if statement in PHP?
- What are the key configurations related to file uploads in PHP that should be checked, such as file_uploads, upload_max_filesize, upload_tmp_dir, post_max_size, and max_input_time?