...

View Full Version : Storing MySQL result set in session



markhartnady
10-18-2004, 04:25 PM
Is this possible? I tried:


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


But it dont work :(

rswyatt
10-18-2004, 04:36 PM
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?

marek_mar
10-18-2004, 05:04 PM
Sessions should only be used to store temporary data. Use a DB or file to store data for longer time periods.

raf
10-18-2004, 08:16 PM
Is this possible? I tried:


$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
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;
?>

AaronW
10-18-2004, 08:23 PM
session_register () does take a string name of the variable, by the way. It doesn't with when register_globals is off though.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum