Page 1 of 1

mysql queries and php sessions

Posted: Wed Aug 15, 2007 10:19 pm
by Faithe

Code: Select all

<?php 

$result = mysql_query("SELECT * FROM bmem WHERE employer=$_SESSION['id']");
while($row = mysql_fetch_array($result))
  {
  echo $row['name'] . " " . $row['age'];
  echo "<br />";
  }

?>
This is the code i'm using to attempt to grab the rows from the table in which the employer's id corresponds with the id of the logged on user. Now, obviously, this is wrong because i'm getting Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING this lovely error.

I know I can't use $_SESSION['id'] because of the error, so i'd appreciate if anyone could point me the direction of the correct way to accomplish the goal.

(Security is little of the essence, although never completely disregarded of course, as none of the information stored is crucial. Most of it is made up, actually.)

Thanks :]

Posted: Wed Aug 15, 2007 10:20 pm
by VladSun

Code: Select all

<?php

$result = mysql_query("SELECT * FROM bmem WHERE employer=".$_SESSION['id']);
while($row = mysql_fetch_array($result))
  {
  echo $row['name'] . " " . $row['age'];
  echo "<br />";
  }

?>

Posted: Wed Aug 15, 2007 10:22 pm
by Faithe
Haha!
How obviously simple.
:]
Thank you.

Re: mysql queries and php sessions

Posted: Wed Aug 15, 2007 10:23 pm
by Christopher
Or use braces around arrays and object->properties:

Code: Select all

$result = mysql_query("SELECT * FROM bmem WHERE employer={$_SESSION['id']}");

Posted: Wed Aug 15, 2007 11:55 pm
by Faithe
Hopefully it's okay to continue on this thread with my next question?
Well, here goes.

Code: Select all

<?php

$result = mysql_query("SELECT * FROM bmem WHERE employer=".$_SESSION['id']);
while($row = mysql_fetch_array($result)) 
{
		echo "<a href=bmem.php?bmid=" .$row['bmid']. ">" .$row['name'];
		echo "<br />";

		$_SESSION['bmid'] = $row['bmid'];
	}
?>
What I am attempting, here, is to take the query results and put them in sessions so when you visit the member's page, the session will express which information to query.

I get two results from the initial query, but the problem is that it shows me the 2nd result's ID on both pages. What i'd like it do is show me each ID specific to the page i'm visiting.

I do hope that makes sense. If not, I can try to explain differently.

Posted: Thu Aug 16, 2007 3:56 am
by miro_igov

Code: Select all

$_SESSION['bmid'][] = $row['bmid'];
To display:

Code: Select all

foreach($_SESSION['bmid'] as $number=>$value) echo $number.'->'.print_r($value);