mysql queries and php sessions

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
Faithe
Forum Commoner
Posts: 33
Joined: Tue Jul 12, 2005 3:26 pm
Location: WA

mysql queries and php sessions

Post 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 :]
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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 />";
  }

?>
There are 10 types of people in this world, those who understand binary and those who don't
Faithe
Forum Commoner
Posts: 33
Joined: Tue Jul 12, 2005 3:26 pm
Location: WA

Post by Faithe »

Haha!
How obviously simple.
:]
Thank you.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: mysql queries and php sessions

Post by Christopher »

Or use braces around arrays and object->properties:

Code: Select all

$result = mysql_query("SELECT * FROM bmem WHERE employer={$_SESSION['id']}");
(#10850)
Faithe
Forum Commoner
Posts: 33
Joined: Tue Jul 12, 2005 3:26 pm
Location: WA

Post 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.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post 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);
Post Reply