What are the advantages and disadvantages of using MySQL instead of text files for storing guestbook entries in PHP?

Using MySQL instead of text files for storing guestbook entries in PHP offers advantages such as better performance, scalability, and data integrity. MySQL allows for efficient querying and indexing of data, making it easier to retrieve and manipulate guestbook entries. However, using MySQL also requires setting up a database server and may introduce additional complexity compared to simply writing to text files.

// 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 record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();