What are the advantages and disadvantages of using a database versus a text file for storing and managing data related to website functionality, such as reservation availability?

When managing data related to website functionality, such as reservation availability, using a database offers advantages such as scalability, data integrity, and better query capabilities. However, databases can be more complex to set up and maintain compared to text files. Text files are simpler to work with but may not be as efficient for storing and managing large amounts of data.

// Example PHP code snippet using a database to store reservation availability data

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

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

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

// Query the database for reservation availability
$sql = "SELECT * FROM availability WHERE date = '2022-01-01'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data for each row
    while($row = $result->fetch_assoc()) {
        echo "Date: " . $row["date"]. " - Availability: " . $row["available"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();