I want to store the result of a MySQLi query as a session variable so that I can reuse it without executing the query again. I don't want to execute the same query on every page of my website or every time a page is refreshed.
I've tried the code below, but I get errors like "object can not be stored in session" and "mysqli_fetch_array expects parameter one to be a resource".
How can I store the query result in a session?
session_start();
if (!isset($_SESSION['query_result'])) {
$anything=mysqli_query($connection,"select something from table name
where filed1='$variable' order by id desc limit 70");
$_SESSION['query_result']=$anything;
} else {
$anything= $_SESSION['query_result'];
}
while ($data=mysqli_fetch_array($anything)) {
/* output the data */
}