What are the advantages and disadvantages of using text files versus a database for storing chat messages in a PHP script?

When deciding between using text files or a database for storing chat messages in a PHP script, the advantage of text files is simplicity and ease of implementation, while the disadvantage is limited querying capabilities and potential performance issues with large amounts of data. On the other hand, databases offer better organization, querying capabilities, and scalability, but require more setup and maintenance.

// Example of storing chat messages in a text file
$message = "Hello, how are you?";
$file = 'chat_messages.txt';
file_put_contents($file, $message . PHP_EOL, FILE_APPEND);

// Example of storing chat messages in a database (MySQL)
$mysqli = new mysqli("localhost", "username", "password", "database");
$message = "Hello, how are you?";
$query = "INSERT INTO chat_messages (message) VALUES ('$message')";
$mysqli->query($query);
$mysqli->close();