SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How do we assign a MySQL array to a SESSION?

    Hello,

    Can one do this:

    $query_chk_word = mysql_query($sql_chk_word) or die(mysql_error());
    $_SESSION['list_found'] = $query_chk_word;

    That is can a MySQK result array be assigned to a Php SESSION?

    If not via above method, how can the results of a mysql_query be saved so that we do not have to again and again
    generate the same?

    Thank you.

    Anoox search engine volunteer

    www.anoox.com

  2. #2
    SitePoint Member lyndonbaptiste's Avatar
    Join Date
    Dec 2012
    Location
    Trinidad and Tobago
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by WorldNews View Post
    Hello,

    Can one do this:

    $query_chk_word = mysql_query($sql_chk_word) or die(mysql_error());
    $_SESSION['list_found'] = $query_chk_word;

    That is can a MySQK result array be assigned to a Php SESSION?

    If not via above method, how can the results of a mysql_query be saved so that we do not have to again and again
    generate the same?

    Thank you.
    WorldNews, you can push the result into an array then serialize and store the array in a session. For example:

    $_SESSION['list_found'] = serialize($arr);

    To access the array again you will need to unserialize the content. For example:

    $arr = unserialize($_SESSION['list_found']);

  3. #3
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lyndonbaptiste View Post
    WorldNews, you can push the result into an array then serialize and store the array in a session. For example:

    $_SESSION['list_found'] = serialize($arr);

    To access the array again you will need to unserialize the content. For example:

    $arr = unserialize($_SESSION['list_found']);
    True, you can do that OR if you like to do the same with less code you can do this:
    Code:
    $_SESSION['list_found'] = $arr;
    $arrFromSession = $_SESSION['list_found'];
    You don't need to call serialize() to put array into session and unserialize when you get it from - php will do it automatically for you.
    All you do is assign array to some session key and then you can get your array back by using that session key.
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  4. #4
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I tried that. But it is not working. To be exact I have (in summary of course):

    $query_chk_word = mysql_query($sql_chk_word) or die(mysql_error());
    $_SESSION['list_found'] = serialize($query_chk_word);

    And upon POST of the page then:

    $query_chk_word = unserialize($_SESSION['list_found']);
    mysql_data_seek($query_chk_word, $start);

    But I get Error message:

    Warning: mysql_data_seek(): supplied argument is not a valid MySQL

    which of course means that $query_chk_word is not being delivered OK!

    So what should I do to correct this?

    ThanX


    Quote Originally Posted by lyndonbaptiste View Post
    WorldNews, you can push the result into an array then serialize and store the array in a session. For example:
    $_SESSION['list_found'] = serialize($arr);

    To access the array again you will need to unserialize the content. For example:

    $arr = unserialize($_SESSION['list_found']);

    Anoox search engine volunteer

    www.anoox.com

  5. #5
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    Just wondering do you guys have any answer to this problem/question of mine?

    Thanks before hand.

    Quote Originally Posted by lampcms.com View Post
    True, you can do that OR if you like to do the same with less code you can do this:
    Code:
    $_SESSION['list_found'] = $arr;
    $arrFromSession = $_SESSION['list_found'];
    You don't need to call serialize() to put array into session and unserialize when you get it from - php will do it automatically for you.
    All you do is assign array to some session key and then you can get your array back by using that session key.

    Anoox search engine volunteer

    www.anoox.com

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Dont store the result object. Store the results from that object.

    while($row = mysql_fetch_row($result) {
    $arr[] = $row;
    }
    $_SESSION['result'] = $arr;

    PS: Look at updating to PDO (or mysqli), as mysql library is being phased out.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #7
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Not sure what you mean!
    To be exact we have a MySQL Query that returns a list of say 1000 rows, with each row having like 10 items.

    What we need to do is to display like 10 items per page and if client clicks on NEXT or BACK then display
    10 more from this list. What we want is not to regenerate this MySQL query, for obvious reasons of not
    putting MORE load on the MySQL server and instead save this MySQL result list in a SESSION or in an Array.

    How can this be done?
    Again there are multiple items per row so a simple Array will not do.

    So I prefer we stick with SESSIONS, but if that is not possible then we would need an
    array like: title[1], body[1], url[1]................. title[1], body[1], url[1]

    Regards,



    Quote Originally Posted by StarLion View Post
    Dont store the result object. Store the results from that object.
    while($row = mysql_fetch_row($result) {
    $arr[] = $row;
    }
    $_SESSION['result'] = $arr;

    PS: Look at updating to PDO (or mysqli), as mysql library is being phased out.

    Anoox search engine volunteer

    www.anoox.com

  8. #8
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by WorldNews View Post
    Not sure what you mean!
    You cannot store resources in sessions that's why your code doesn't work. mysql_query() returns a resource - a pointer to a result set that is available at the current MySQL session (connection). Because on each page request you have to connect to the db again you cannot use the pointer to the old session, which has been already destroyed. What you want to accomplish is not possible.

    You have to fetch all the data into arrays (or objects) and then store those arrays in the session. However, if you are going to store a lot of data in the session then it might be more reasonable to query the database - and use some caching if necessary.

  9. #9
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by WorldNews View Post
    Hi,

    Not sure what you mean!
    To be exact we have a MySQL Query that returns a list of say 1000 rows, with each row having like 10 items.

    What we need to do is to display like 10 items per page and if client clicks on NEXT or BACK then display
    10 more from this list. What we want is not to regenerate this MySQL query, for obvious reasons of not
    putting MORE load on the MySQL server and instead save this MySQL result list in a SESSION or in an Array.
    And here's where I stick my foot in my mouth with r937 and the guys over in the database forums when I say: It's better to do several small queries for the data you -need- as opposed to a massive query for data you may or may not need.
    IE: If you only display 10 rows, and the user never clicks on the other pages, you've pushed 990 rows of data for no reason.

    How can this be done?
    Again there are multiple items per row so a simple Array will not do.
    Sure it will.
    $_SESSION['result'][0] would hold an array, which has items ['title'],['body'], etc. It's a simple two-dimensional array (which... is in another array, so technically it's 3 dimensional).
    So I prefer we stick with SESSIONS, but if that is not possible then we would need an
    The above code uses sessions.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  10. #10
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes, that is what I was afraid off that it cannot be done

    Anoox search engine volunteer

    www.anoox.com

  11. #11
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes, I think we will try putting them in an array().

    NOTE: I think there is an Error in MySQL or shortcoming at least.
    That is in case of SELECT like:

    SELECT id, ix_id, title, descpt, category FROM user_data WHERE MATCH (title, descpt) AGAINST('$find') LIMIT 90, 100

    does not work. That is LIMIT 90, 100 does not work, as it returns 100 results rather than LIMIT 90, 100!

    I wonder what MySQL community thinks of this!

    Anoox search engine volunteer

    www.anoox.com

  12. #12
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,067
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by WorldNews View Post
    Yes, I think we will try putting them in an array().

    NOTE: I think there is an Error in MySQL or shortcoming at least.
    That is in case of SELECT like:

    SELECT id, ix_id, title, descpt, category FROM user_data WHERE MATCH (title, descpt) AGAINST('$find') LIMIT 90, 100

    does not work. That is LIMIT 90, 100 does not work, as it returns 100 results rather than LIMIT 90, 100!

    I wonder what MySQL community thinks of this!
    This is not an error, this is intended behavior.

    The syntax is LIMIT <start>, <number of results>.

    So 90, 100 gives you 100 rows starting at row 89 (it's 0-based). If you want rows 90-100 you should use LIMIT 89,10.

    http://dev.mysql.com/doc/refman/5.5/en/select.html
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  13. #13
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    So 90, 100 gives you 100 rows starting at row 91 (it's 0-based). If you want rows 90-100 you should use LIMIT 89,10.
    fix-ed it
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  14. #14
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,067
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by StarLion View Post
    fix-ed it
    Right, my bad
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  15. #15
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    So this is going to take us back to then issuing 2 SELECTs.
    1st SELECT to tell us, so we can tell users, how many total results are so that we can say for example page 1 of 35, etc.
    And then the 2nd SELECT will actually step 10 rows per page.

    FYI, I was hoping to limit the SELECT once per query since these queries can be very (mysql) server load intensive.
    But that does not seem to be possible.


    Quote Originally Posted by ScallioXTX View Post
    Right, my bad

    Anoox search engine volunteer

    www.anoox.com

  16. #16
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I did what you suggested.
    But for some reason that is too hard to explain, I need to check a certain value of the 1st row of each result set.
    And as we know as one does this MySQL index array moves up by 1.
    So I need to bring this pointed back to where it was, which I am doing with:

    mysql_data_seek($query_chk_word, $start);

    this works fine in case of LIMIT 0, 15

    but fails with this error message in subsequent pages, such as:

    this works fine in case of LIMIT 15, 15

    Warning: mysql_data_seek() [function.mysql-data-seek]: Offset 15 is invalid for MySQL result index 8 (or the query data is unbuffered) in /var/www/html/anoox.com/find_new.php on line 293

    and I see that then the results are displayed starting from row 18!

    You know what the hek is going on?

    ThanX,

    Anoox search engine volunteer

    www.anoox.com

  17. #17
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by WorldNews View Post

    mysql_data_seek($query_chk_word, $start);

    this works fine in case of LIMIT 0, 15

    but fails with this error message in subsequent pages, such as:

    this works fine in case of LIMIT 15, 15

    Warning: mysql_data_seek() [function.mysql-data-seek]: Offset 15 is invalid for MySQL result index 8 (or the query data is unbuffered) in /var/www/html/anoox.com/find_new.php on line 293
    What value did you pass to mysql_data_seek()? You need to remember that for mysql_data_seek() rows always start at 0 no matter what values you put in LIMIT because it does not understand LIMIT or any SQL at all. So even when you use LIMIT 15, 15 the valid values for mysql_data_seek() are 0 to 14 - or even less than 14 if the query returned fewer rows than 15.

  18. #18
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ah So,
    Das macht sehr gut sinn!

    In English: that makes good sense

    Ich wünsche Ihnen einen guten Tag.
    Have a very good day

    ThanX.


    Quote Originally Posted by Lemon Juice View Post
    What value did you pass to mysql_data_seek()? You need to remember that for mysql_data_seek() rows always start at 0 no matter what values you put in LIMIT because it does not understand LIMIT or any SQL at all. So even when you use LIMIT 15, 15 the valid values for mysql_data_seek() are 0 to 14 - or even less than 14 if the query returned fewer rows than 15.

    Anoox search engine volunteer

    www.anoox.com


Bookmarks

Posting Permissions

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