What are the advantages and disadvantages of using a database instead of a CSV file for storing log data in PHP?

When storing log data in PHP, using a database instead of a CSV file has several advantages such as better data organization, faster data retrieval, and the ability to easily query and analyze the data. However, using a database can also be more resource-intensive and complex to set up compared to simply writing to a CSV file.

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

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

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

// Insert log data into the database
$log_message = "This is a log message";
$sql = "INSERT INTO logs (message) VALUES ('$log_message')";

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

// Close the database connection
$conn->close();