1

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 */
}
showdev
  • 28,454
  • 37
  • 55
  • 73
Rahul aman
  • 379
  • 3
  • 13
  • please see the updated question ... i am sorry i put () for desc it... in actual code i did it correct and i have started the session on the first line .. – Rahul aman Apr 13 '15 at 17:12
  • What does your mysqli_fetch_array code look like. You can't store values in session without calling one of the mysqli_fetch_ functions. – Maximus2012 Apr 13 '15 at 17:16
  • $_SESSION['query_result']=$anything; is definitely not going to work. – Maximus2012 Apr 13 '15 at 17:18
  • mysqli_fetch_array prinnts the name and email of selected people by the query . these names are to be displayed on every page of site and dosent change often .... – Rahul aman Apr 13 '15 at 17:19
  • 1
    You can't store a mysql resource in a $_SESSION. Objects need to be serialized to be stored as session variables, and resources can't be reliably serialized. I suggest storing the actual data you need in the session, or performing the query upon each page load. Keep in mind that the mysql server will attempt to [cache identical queries](https://dev.mysql.com/doc/refman/5.7/en/query-cache.html), which will speed up subsequent identical queries. – showdev Apr 13 '15 at 17:19
  • yes !!! it is not working ... i know .. – Rahul aman Apr 13 '15 at 17:20
  • 1
    [This post](http://stackoverflow.com/questions/24874442/using-php-session-variables-to-store-mysql-query-results) and its [related posts](http://stackoverflow.com/questions/7083267/can-i-store-a-class-instance-in-a-session-space) might be helpful. – showdev Apr 13 '15 at 17:20

3 Answers3

1

If you want to store the data not only for one client, but serverside I would recommend you to look at how to build a in-memory server side cache in php?. Some frameworks have this already built in.

Otherwise use one of the mysqli API methods like http://php.net/manual/de/mysqli-result.fetch-assoc.php, this should return you an array not an object.

mysqli_query() returns an object which you can not store into session (there is a method for converting an object to an array, but I would not recommend you to store the whole object) http://php.net/manual/de/function.get-object-vars.php

Community
  • 1
  • 1
ju_
  • 569
  • 1
  • 4
  • 17
0

Try this:

  if(!isset($_SESSION['query_result'])){
     $anything=mysqli_query($connection,"select something from table name where filed1='$variable' order by id DESC limit 70");
     while($res = mysqli_fetch_row($anything)){
        array_push($_SESSION['query_result'], $res);
     }
  } else {
     $anything = $_SESSION['query_result'];
  }
while($data=mysqli_fetch_array($anything)){
   print_r($data);
}
Ashwani Goyal
  • 616
  • 4
  • 18
  • A description of your solution would be helpful. – showdev Apr 13 '15 at 17:27
  • Why not explain what you've done? It wouldn't take any longer than it did to add your comment and it would be helpful to people who come across your answer in the future. – showdev Apr 15 '15 at 18:39
0

Use like this:

$result = $connection->query("select something from table");
$_SESSION['session-name'] = $result->fetch_assoc();

OR

$sql = `select something from table`;
$result = mysqli_query($sql);
$_SESSION['session name'] = mysqli_fetch_assoc($result);
Nikolay Mihaylov
  • 3,868
  • 8
  • 27
  • 32
Abdul Haq
  • 1
  • 1