Is it more efficient to store user profiles in a database rather than generating individual PHP files for each user?
Storing user profiles in a database is more efficient than generating individual PHP files for each user. With a database, you can easily query and retrieve user data, update profiles, and perform other operations efficiently. It also allows for better organization and scalability as your user base grows.
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
// Query the database to retrieve user profile
$user_id = 1;
$sql = "SELECT * FROM users WHERE id = $user_id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
echo "Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
Keywords
Related Questions
- How can PHP be used to manipulate HTTP headers and mimic a browser during a request?
- How can you access class variables within a constructor or other functions in PHP?
- What role does proper error handling play in troubleshooting PHP scripts, especially when dealing with database connections and queries?