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 />";
  }
pixelscapeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dejaanbuCommented:
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_r($row);
print '</pre>';

Open in new window


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

$result = mysql_query("SELECT * FROM product
WHERE finish= '".$_SESSION['finish'].'");
0
Rik-LeggerCommented:
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?
0
Scott MadeiraCommented:
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']}");
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pixelscapeAuthor Commented:
perfect... added a double quote to what you wrote and it works fine...

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

thx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.