How can database integration improve the functionality and efficiency of a PHP-based CMS compared to directly outputting data in files?
Database integration can improve the functionality and efficiency of a PHP-based CMS by allowing for dynamic content management, easier data retrieval and manipulation, and better scalability. By storing data in a database instead of directly outputting it in files, the CMS can easily handle large amounts of data and provide faster access to information.
// Connect to the 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 the database for content
$sql = "SELECT * FROM content";
$result = $conn->query($sql);
// Output the content
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<h2>" . $row["title"] . "</h2>";
echo "<p>" . $row["content"] . "</p>";
}
} else {
echo "No content found";
}
// Close the database connection
$conn->close();