generate $_SESSION variables from database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
ghostsquad
Forum Newbie
Posts: 3
Joined: Thu May 31, 2007 9:22 pm

generate $_SESSION variables from database

Post by ghostsquad »

What I have is a small table in a database with a couple columns including "name", "label", and "used". My goal is to make a generic "contact" page for my clients and modify just the database to display and use the fields they want.

The only problem I'm having with this is creating some of the session variables I need.

Code: Select all

$query_used = "SELECT * FROM `form` WHERE `used`=1";
$result_used = mysql_query( $query_used ) or die ("could not query the database: <br />". mysql_error());

while ($row = mysql_fetch_assoc( $result_used )){
		$name = $row['name'];
		// name corresponds to: <input name="first" ...

		$_SESSION[$name] = htmlentities($_POST[$name]);
		
	}
so for instance, theoretically I should be getting

Code: Select all

$_SESSION['first'] = "bob";
$_SESSION['last'] = "anderson";
$_SESSION['email'] = "bob@domain.ext";  
// etc as it hands out the results of the query

// keep in mind the results of the query will exactly match that of the $_POST data, as the fields are generated in the same manner.
but its not even saying the variables are set, let alone that they have a value.

Other session variables I set manually work though, like:

Code: Select all

$ip = getenv("REMOTE_ADDR");
$_SESSION['ip'] = $ip;

// or

$_SESSION['msg'] = htmlentities($_POST['msg']);

// these are set manually (no loop).
I don't get whats going on. Anyone have any ideas?
ghostsquad
Forum Newbie
Posts: 3
Joined: Thu May 31, 2007 9:22 pm

Post by ghostsquad »

ok I figured out the problem.. which this is another question that is bugging me...

I use the same query and results multiple times in my page

Code: Select all

$query_used = "SELECT * FROM `form` WHERE `used`=1";
$result_used = mysql_query( $query_used ) or die ("could not query the database: <br />". mysql_error());
I know the $query_used variable doesn't need to be repeated, but when the $result_used variable is needed multiple times, do I need to repeat that variable each time. Example:

Code: Select all

$query_used = "SELECT * FROM `form` WHERE `used`=1";
$result_used = mysql_query( $query_used ) or die ("could not query the database: <br />". mysql_error());

  while ($row = mysql_fetch_assoc( $result_used )){
	$name = $row['name'];
	$label = $row['label'];
	$req = $row['req'];
	
	if (($_POST[$name] == "") && ($req == 1)){
		$error .="Please input your <b>$label</b>.<br />\n";
	}		
  }

// $result_used = mysql_query( $query_used ) or die ("could not query the database: <br />". mysql_error());

	while ($row = mysql_fetch_assoc( $result_used )){
		$name = $row['name'];
		
		$_SESSION[$name] = htmlentities($_POST[$name]);
		
	}
this doesn't seem to work unless I uncomment out the 2nd variable. why is that?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The second while loop will not run due to mysql_fetch_assoc() returning false because that's how the first while loop ended. Combine them if possible. If not, mysql_data_seek() may be of interest.
ghostsquad
Forum Newbie
Posts: 3
Joined: Thu May 31, 2007 9:22 pm

Post by ghostsquad »

I understand now.

in my terms, correct me if I'm wrong but.. $result is the simply the result of the query as a resource waiting to be used.

by calling mysql_fetch_row.. it "uses" the resource by moving the internal pointer (that points to each row) down during the while loop.
At the end of the while loop, the pointer is at the bottom, therefore any other attempts to use that same resource won't work or rather will attempt to continue to move that pointer down, but there is nothing else to query.

The way to get around this is either to refresh the resource all together by redeclaring the
$result_used = mysql_query( $query_used ) or die ("could not query the database: <br />". mysql_error());
OR
mysql_data_seek($result, 0);

which the latter simply tells the pointer to go back up to the top.

Is that correct?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Yes.
Post Reply