Use session variable in a database query

I have a session variable at ... $_SESSION['finish']

Following is part of my database script... using the variable as is does not work... is it because the finish table field and variable name confict? Or do I need to do something with the variable before inserting it?

$result = mysql_query("SELECT * FROM product
WHERE finish='.$_SESSION['finish'].'");

while($row = mysql_fetch_array($result))
  echo $row['productcode'] . " " . $row['finish'];
  echo "<br />";
Who is Participating?
Scott MadeiraConnect With a Mentor Commented:
My previous reply assumes that the teh session variable is a string or some non-numeric data that needs single quotes around it.  It it is numeric (and doesn't need to be quoted then you should be able to do this:

$result = mysql_query("SELECT * FROM product WHERE finish={$_SESSION['finish']}");
couldnt get ur problem...did u get rows returned from db ?

you have to give the same column name in the row index...if finish is ur column name, then it should  work.

give a try

print '<pre>';
print '</pre>';

Open in new window

Scott MadeiraCommented:
try this...  I think it was a quotes issue...

$result = mysql_query("SELECT * FROM product
WHERE finish= '".$_SESSION['finish'].'");
If you echo $_SESSION['finish']; do you see a value?
There is no naming conflict so the problem has to be somewhere else.

You used session_start(); in the beginning of your script?
pixelscapeAuthor Commented:
perfect... added a double quote to what you wrote and it works fine...

WHERE finish= '".$_SESSION['finish'].'");

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.