会话变量 - mysqli_query结果

I am having an issue with saving a result object in a session variable. I am wondering if there is a way to save the result object (not the actual results) in a session variable so that I can use it in other areas of my site without having to make another trip to the DB. In theory, im trying to do the following:

$formQ2 = mysqli_query($dbconnection, $formQuery3); 
$_SESSION['test'] = $formQ2;

I then want to use the session variable like this:

while ($rows = mysqli_fetch_assoc($_SESSION['test'])){...}

When I run this it is bringing back an empty object instead of the result object. Again, I am not trying to save the actual results to the session but the result object so that i can loop through it on other pages. Thank you.

Just so that you know if I do the follow, it works fine. just so that you know the rest of my code is correct.while ($rows = mysqli_fetch_assoc($formQ2)){...} it almost as iff the session is not setting because when i use a hard coded variable it works fine. Thanks.

EDIT - My entire code. So when I run it the first time (when the session is empty and being set for the first time) it works fine. So it holds the session for one use. When i refresh the page (same page not changes) i get the error as if refreshing the page is unsetting my session var. MY entire code:

if (empty($_SESSION['test'])) {
    $formQuery = "SELECT * FROM 'testTable'";
    $_SESSION['test'] = mysqli_query($dbconnection, $formQuery);
    echo "new sess";
   } else {
       $_SESSION['test'] = $_SESSION['test'];
       echo "old sess";
   }

 while ($rows = mysqli_fetch_assoc($_SESSION['test'] )){...}

As I stated, wehn it runs the if statement for the first time it works fine, but then when it triggers the else statement (page refresh) it returns an empty session variable even though it was set before and worked fine. The error Im getting on refresh is:

Warning: mysqli_fetch_assoc(): Couldn't fetch mysqli_result in xxx on line 
362

It's strange to me that on first load it sets the session and works just fine. But when I simply hit the refresh button it throws and error. Thanks for your help.

I am wondering if there is a way to save the result object (not the actual results) in a session variable

No, there is not. To be able to store values in text form, PHP needs to "serialize" them, and in the context of a session that happens in pretty much the same way as with the serialize function, and in the description of the value parameter for that you find:

The value to be serialized. serialize() handles all types, except the resource type.

And the return value of mysql_query happens to be a resource of type mysql result.

But you can store the result of mysqli_fetch_assoc in the session, because that is just an array, and those can be serialized perfectly fine. But mysqli_fetch_assoc fetches one row out of the result set only - so you will either have to still use a loop to put all the values into your session the first time when you actually query the database - or you use mysqli_fetch_all all instead.

Check the code below. I have an example for you that's working for me. Make sure that you have started session before you do anything.

<?php
    session_start();

    $dbconnection = mysqli_connect("localhost","root","root","myDB");

    $formQuery3 = "SELECT * FROM myTable";
    $formQ2 = mysqli_query($dbconnection,$formQuery3);

    $_SESSION['test'] = $formQ2;

    while ($rows = mysqli_fetch_row($_SESSION['test'])){
        print_r($rows);
    }
?>

Here's what I use:

$_SESSION['valueToSearch'] = $_GET['valueToSearch'];