session variable losing value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ssethia
Forum Newbie
Posts: 2
Joined: Thu Sep 03, 2009 1:33 pm

session variable losing value

Post by ssethia »

Hi,

I have a problem with session variable getting reset.
I have <?php session_start(); ?> on all the pages
File 1 index.php has 2 session variables

$_SESSION['dbhit'] = 0;
$_SESSION['dbresult'] = 0;

File 2 catalog.php has the following code

if($_SESSION['dbhit']==0){
$queryCatalog = 'SELECT catalog.page_id,catalog.name,products.duration,products.dates,products.who,products.prerequisite,products.university FROM `products`,`catalog` WHERE products.page_id = catalog.page_id and catalog.type = "'.$type.'"ORDER BY Rand()';

$resultCatalog = mysql_query($queryCatalog) or die('Query failed: ' . mysql_error());
$_SESSION['dbresult'] = $resultCatalog;
$_SESSION['dbhit'] = 1;
}

$result = $_SESSION['dbresult'];
echo $_SESSION['dbresult'];

now the control moves to File 3 product.php which has a back button, on click of the back button control comes back to file 2 catalog.php, now the session variable $_SESSION['dbresult']; is losing its value.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: session variable losing value

Post by flying_circus »

I'm not sure you really understand what is going on here. It looks as though you are trying to save a resource datatype into a session variable, which can only accept strings or arrays. See, it's not losing the data, you're trying to assign a session variable something it doesnt understand.

Why are you trying to store a query result set to a session variable anyways?

Why dont you just store your query variables (like your $type variable) in a session or pass them along through an http querystring, then let the query be run on the 3rd page to output the results?
ssethia
Forum Newbie
Posts: 2
Joined: Thu Sep 03, 2009 1:33 pm

Re: session variable losing value

Post by ssethia »

flying_circus wrote:I'm not sure you really understand what is going on here. It looks as though you are trying to save a resource datatype into a session variable, which can only accept strings or arrays. See, it's not losing the data, you're trying to assign a session variable something it doesnt understand.

Why are you trying to store a query result set to a session variable anyways?

Why dont you just store your query variables (like your $type variable) in a session or pass them along through an http querystring, then let the query be run on the 3rd page to output the results?
Thanks for ur reply, the only concern here is the query is order by rand() so i cannot query the database more than once or else it would get me different result set each time.

SELECT catalog.page_id,catalog.name,products.duration,products.dates,products.who,products.prerequisite,products.university FROM `products`,`catalog` WHERE products.page_id = catalog.page_id and catalog.type = "'.$type.'"ORDER BY Rand()

On page 2 catalog.php this is the query which runs for the first time and it should not run more than once.. plz help
Post Reply