What is the correct way to retrieve a specific value from a MySQL database using PHP sessions?

To retrieve a specific value from a MySQL database using PHP sessions, you need to first establish a database connection, retrieve the value based on a query, and store it in a session variable. This allows you to access the value across different pages within the same session.

<?php
// Start the session
session_start();

// Establish a database connection
$connection = mysqli_connect("localhost", "username", "password", "database");

// Check connection
if (!$connection) {
    die("Connection failed: " . mysqli_connect_error());
}

// Retrieve a specific value from the database
$query = "SELECT column_name FROM table_name WHERE condition";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
$value = $row['column_name'];

// Store the retrieved value in a session variable
$_SESSION['specific_value'] = $value;

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