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.
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]
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.
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
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.
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!
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.