In PHP, how can session variables be effectively utilized to compare and display data from a database table?
To effectively utilize session variables to compare and display data from a database table in PHP, you can store the necessary data in session variables after retrieving it from the database. Then, you can compare the session variables to display the relevant information on the webpage.
<?php
// Start the session
session_start();
// Assume you have retrieved data from a database table and stored it in $row variable
$row = array(
'id' => 1,
'name' => 'John Doe',
'email' => 'john.doe@example.com'
);
// Store the data in session variables
$_SESSION['id'] = $row['id'];
$_SESSION['name'] = $row['name'];
$_SESSION['email'] = $row['email'];
// Compare and display the data
if ($_SESSION['id'] == 1) {
echo "Name: " . $_SESSION['name'] . "<br>";
echo "Email: " . $_SESSION['email'];
}
?>