What are the advantages and disadvantages of using MySQL over file operations for tasks like managing guestbook entries in PHP?

When managing guestbook entries in PHP, using MySQL has several advantages over file operations. MySQL allows for faster and more efficient data retrieval and manipulation, provides better data organization and indexing capabilities, and offers built-in security features to prevent data corruption or unauthorized access. However, using MySQL also requires setting up and maintaining a database server, which may add complexity to the project.

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

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

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

// Insert guestbook entry into MySQL database
$name = $_POST['name'];
$message = $_POST['message'];

$sql = "INSERT INTO entries (name, message) VALUES ('$name', '$message')";

if ($conn->query($sql) === TRUE) {
    echo "New entry added successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();