What is the best practice for storing query results in a variable for later use in PHP?

When storing query results in a variable for later use in PHP, it is best practice to fetch the data from the database using a query and then store the result in a variable for easy access. This can be achieved by using the mysqli_fetch_assoc() function to fetch an associative array representing the fetched row from the database.

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

// Execute a query
$query = "SELECT * FROM table";
$result = mysqli_query($connection, $query);

// Fetch the result into an associative array
$data = mysqli_fetch_assoc($result);

// Store the data in a variable for later use
$storedData = $data;

// Close the connection
mysqli_close($connection);