What are the advantages and disadvantages of using sessions versus databases to store temporary data in PHP?

Storing temporary data in sessions is convenient and easy to implement in PHP, but it can consume server resources and may not be suitable for large amounts of data. On the other hand, storing temporary data in databases allows for scalability and better organization of data, but it requires additional setup and maintenance.

// Using sessions to store temporary data
session_start();
$_SESSION['temp_data'] = 'example data';

// Using databases to store temporary data
// Connect to database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Insert temporary data into database
$mysqli->query("INSERT INTO temp_data_table (data) VALUES ('example data')");

// Close database connection
$mysqli->close();