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.

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:


$_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.

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

Hello,

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

Thanks before hand.

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.

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,

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

The above code uses sessions.

Yes, that is what I was afraid off that it cannot be done :frowning:

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

fix-ed it :wink:

Right, my bad :blush:

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.

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,

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.

Ah So,
Das macht sehr gut sinn!

In English: that makes good sense :slight_smile:

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

ThanX.