dprichard Posted June 13, 2007 Share Posted June 13, 2007 I have a query that is based on a session variable. If I echo out the session variable, it works fine, but if I use it in a query it doesn't work. Can you use a variable in a query like this? $username = $_SESSION['MM_Username']; $user = mysql_query("SELECT emp_id, emp_fname, emp_lname, emp_auth_level FROM employee WHERE emp_username = $username"); $row_user = mysql_fetch_array($user); Quote Link to comment https://forums.phpfreaks.com/topic/55426-problem-using-a-session-in-a-query/ Share on other sites More sharing options...
Psycho Posted June 13, 2007 Share Posted June 13, 2007 You need to put single ticks around a string value when used in a query: $username = $_SESSION['MM_Username']; $user = mysql_query("SELECT emp_id, emp_fname, emp_lname, emp_auth_level FROM employee WHERE emp_username = '$username'"); $row_user = mysql_fetch_array($user); Quote Link to comment https://forums.phpfreaks.com/topic/55426-problem-using-a-session-in-a-query/#findComment-273938 Share on other sites More sharing options...
franknu Posted June 13, 2007 Share Posted June 13, 2007 your problem is at the end of the query it should be something like this WHERE emp_username= ($_SESSION['username']) Quote Link to comment https://forums.phpfreaks.com/topic/55426-problem-using-a-session-in-a-query/#findComment-273940 Share on other sites More sharing options...
Yesideez Posted June 13, 2007 Share Posted June 13, 2007 hahaha here's a third method (which I use) just to confuse things... $user = mysql_query("SELECT emp_id, emp_fname, emp_lname, emp_auth_level FROM employee WHERE emp_username = '".$username."'"); Although I prefer to use the user's ID number instead of the username. Quote Link to comment https://forums.phpfreaks.com/topic/55426-problem-using-a-session-in-a-query/#findComment-273941 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.