Page 1 of 1

session-handling

Posted: Sat Mar 10, 2007 12:08 pm
by j0elByte
I have this php-code that writes out the querytime and no. of hits from a mySQL-search on the very same page.
Problem is, it's one step behind. That is, when I enter the searchpage and hit search, It doesnt show up, but if i hit search button again it'll show up, showing me the values of the previous search.
I can't find the problem here...

On the 'main' page, Ill fetch the info, if the sessionvar is set, else there havent been a query yet..

Code: Select all

if(isset($_SESSION['querytime']))
			{
				echo "<p>Sökningen tog <strong>". $_SESSION['querytime'] ."</strong> sekunder</p>";
				echo "Antal träffar : " . $_SESSION['hits'];
			}
		?>
One of the session var is set in querytires();

Code: Select all

function querytires($params)
	{
		connect();
		
		$query="
  		SELECT fabrikat,monster,li,ss,dimension,typ,bto_pris,nto_pris,
  		fsg_pris,lagersaldo,img_url,leverantor,info_txt FROM dack";
		$search_p = array();
		foreach($params AS $name=>$value)
		{
			if($value!=""){
				array_push($search_p, "$name = '$value'");
			}
		}
		if(count($search_p)>0)
			$query .= "WHERE ".implode(" AND ", $search_p);

		$start_t = getTime();
		$results = mysql_query($query) or die(mysql_error());
		$stop_t = getTime();
		$total = $stop_t - $start_t;
		$_SESSION['querytime'] = round( $total, 4);
				
		$restable = render_table($results);	
		return $restable;
	}
(render_table() just returns a table with the searchresults, it doesnt modify the session variabel)

Any point in the right direction would be helpful.. Im very new to php..:-/

Posted: Sat Mar 10, 2007 12:18 pm
by arukomp
Did you put session_start() on every page? It has to be putted before accessing and setting $_SESSION variables.

Posted: Sat Mar 10, 2007 12:26 pm
by feyd
If your "querytime" information is run prior to all the queries being run the data will not be available until the second page as you have said.

Posted: Sat Mar 10, 2007 2:17 pm
by j0elByte
feyd wrote:If your "querytime" information is run prior to all the queries being run the data will not be available until the second page as you have said.
Hmm I think its my logic thinking that fails me a bit here... Youre right, the query is being done after the session variables are set. which means that the information is one post too old.. Now i get it.. So I should switch place on those two in the design?
Thanks

Posted: Sat Mar 10, 2007 2:18 pm
by j0elByte
arukomp wrote:Did you put session_start() on every page? It has to be putted before accessing and setting $_SESSION variables.
Yup.. Thanks anyway