0
votes

I have a Session that stores data for a user when they login, but I can only print the data. Like so...

echo print_r($_SESSION);

WILL RETURN...

Array ([user_id] => 5 [access_level] => 4 [user_name] => user1 [division] => NEB [dept] => ALL) 1

But when I enter code like this...

$sql = "SELECT * FROM table where division = '".$_SESSION['division']."' ORDER BY status";

And do an echo of that statement, it looks like this...

SELECT * FROM table where division = '' ORDER BY status

Unless I echo the session variable, the session data disappears!

Please advise.

2
is $SESSION a typo? Post your code and maybe we can help. - Tim Withers
Are you sure that you did the session_start(); ? - Erman Belegu
Yes, it was an has been corrected. Good looking out, TW. - FroNuff
Thanks Erman, the session_start was missing from a header php file. - FroNuff
If session_start was missing, how could print_r() print it correctly? Are those two statements in different scripts? - Barmar

2 Answers

0
votes
$sql = "SELECT * FROM `table` WHERE division = '{$_SESSION['division']}' ORDER BY status";
0
votes

It's probably better to do it like this

$division = $_SESSION['division'];
$sql = "SELECT * FROM table where division = '$division' ORDER BY status";

Surprising but it will work.