Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.