How can PHP developers effectively utilize WHERE clauses in SQL queries to filter data based on user input or session variables?

When PHP developers need to filter data in SQL queries based on user input or session variables, they can effectively utilize WHERE clauses. By dynamically constructing the WHERE clause based on the user input or session variables, developers can ensure that only the relevant data is retrieved from the database.

// Assume $userInput or $sessionVariable contains the value to filter
$userInput = $_POST['user_input']; // Example of user input from a form
$sessionVariable = $_SESSION['user_id']; // Example of session variable

// Construct the SQL query with a WHERE clause based on user input or session variable
$sql = "SELECT * FROM table_name WHERE column_name = '" . $userInput . "' OR session_column = '" . $sessionVariable . "'";

// Execute the SQL query and fetch the results
$result = mysqli_query($connection, $sql);

// Process the results as needed
while ($row = mysqli_fetch_assoc($result)) {
    // Do something with the retrieved data
}