store mysqli_query result in session

最后都变了- 提交于 2019-12-02 05:01:43

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);
}
ju_

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

Abdul Haq

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!