dsqa6272
2017-06-09 23:44
浏览 80
已采纳

会话变量 - 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.

图片转代码服务由CSDN问答提供 功能建议

我遇到了将结果对象保存在会话变量中的问题。 我想知道是否有一种方法可以在会话变量中保存结果对象(而不是实际结果),这样我就可以在我的站点的其他区域使用它,而无需再次访问数据库。 理论上,我试图做以下事情:

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

然后我想使用会话变量: < pre> while($ rows = mysqli_fetch_assoc($ _ SESSION ['test'])){...}

当我运行它时它会带来 返回一个空对象而不是结果对象。 同样,我不是试图将实际结果保存到会话而是结果对象,以便我可以在其他页面上循环它。 谢谢。

只是为了让您知道我是否遵循了这一点,它运行正常。 只是为了让你知道我的其余代码是正确的。 while($ rows = mysqli_fetch_assoc($ formQ2)){...} 它几乎就像会话没有设置因为当我使用一个 硬编码变量它工作正常。 谢谢。

编辑 - 我的整个代码。 因此,当我第一次运行它时(当会话为空并且第一次设置时)它运行正常。 所以它持有会话供一次使用。 当我刷新页面(同一页面没有更改)时,我得到错误,好像刷新页面是取消我的会话var。 我的整个代码:

  if(empty($ _ SESSION ['test'])){
 $ formQuery =“SELECT * FROM'testTable'”; 
 $ _SESSION  ['test'] = mysqli_query($ dbconnection,$ formQuery); 
 echo“new sess”; 
} else {
 $ _SESSION ['test'] = $ _SESSION ['test']; 
 echo“ 旧的sess“; 
} 
 
 while($ rows = mysqli_fetch_assoc($ _ SESSION ['test'])){...} 
   
 
 

As 我说,我们第一次运行if语句它运行正常,但是当它触发else语句(页面刷新)时,它返回一个空的会话变量,即使它之前设置并且工作正常。 我在刷新时遇到的错误是:

 警告:mysqli_fetch_assoc():无法在xxx行上获取mysqli_result 
 
 
    \  n 
 

我很奇怪,在第一次加载时它会设置会话并且工作得很好。 但是,当我只是按下刷新按钮时,它会抛出并出错。 谢谢你的帮助。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongzhan2029 2017-06-10 02:03
    已采纳

    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.

    打赏 评论
  • duangou6446 2017-06-10 00:23

    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);
        }
    ?>
    
    打赏 评论
  • doutui9606 2017-06-10 02:52

    Here's what I use:

    $_SESSION['valueToSearch'] = $_GET['valueToSearch'];
    
    打赏 评论

相关推荐 更多相似问题