How can PHP sessions be properly utilized to filter and display specific data from a MySQL database?

To filter and display specific data from a MySQL database using PHP sessions, you can store the filter criteria in a session variable and use it in your SQL query to retrieve the desired data. By setting the session variable with the filter criteria, you can maintain the filter across different pages without the need to pass it through the URL or form submissions.

<?php
session_start();

// Set the filter criteria in a session variable
$_SESSION['filter'] = 'category_id = 1';

// Connect to the database
$connection = mysqli_connect('localhost', 'username', 'password', 'database');

// Retrieve data based on the filter criteria
$query = "SELECT * FROM table_name WHERE " . $_SESSION['filter'];
$result = mysqli_query($connection, $query);

// Display the data
while ($row = mysqli_fetch_assoc($result)) {
    echo $row['column_name'] . '<br>';
}

// Close the database connection
mysqli_close($connection);
?>