Jump to content

Problem using a session in a query


dprichard

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/55426-problem-using-a-session-in-a-query/
Share on other sites

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.