Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Storing MySQL result set in session

    Is this possible? I tried:

    PHP Code:
    $result mysql_query($sql);
    $sess_result $result;
    session_register("sess_result"); 
    But it dont work

  • #2
    Regular Coder
    Join Date
    Jan 2004
    Location
    Des Moines, Iowa
    Posts
    219
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What about it doesn't work?

    $result = mysql_query($sql);
    $sess_result = $result;
    session_register("sess_result");


    There are a myriad of reasons why this doesn't work.

    Did you start the page with session_start(); ?

    Are you trying to get this variable on another page? If so - see the first question.

    How are you attempting to get the results of the query? Are you using mysql_fetch_array or something similar to extract the array?
    Rich S. Wyatt
    D3 Web Creations

  • #3
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Sessions should only be used to store temporary data. Use a DB or file to store data for longer time periods.

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by markhartnady
    Is this possible? I tried:

    PHP Code:
    $result mysql_query($sql);
    $sess_result $result;
    session_register("sess_result"); 
    But it dont work
    i think it's obvious why it doesn't work.
    First, session_register("sess_result"); , it's outdated, depreciated syntax + you are setting it to "sess_result", where you wanted to set it to $sess_result.
    Second, $result isn't the actual returned resultset. It's just an id (like ('12' or so) that you can use to refer to the actual resultset.

    Working code would be
    PHP Code:
    <?php
    session_start
    (); // <-- put this at the very first line
    ...
    $result mysql_query($sql);
    $arr_resultset = array();
    if (
    mysql_num_rows($result) >= 1){
       while (
    $arr_resultset[] = mysql_fetch_assoc($result)){}
    }
    $_SESSION['resultset'] = $arr_resultset;
    ?>
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #5
    Senior Coder
    Join Date
    Feb 2003
    Location
    Ontario, Canada
    Posts
    1,223
    Thanks
    0
    Thanked 0 Times in 0 Posts
    session_register () does take a string name of the variable, by the way. It doesn't with when register_globals is off though.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •