Link to home
Start Free TrialLog in
Avatar of pixelscape
pixelscape

asked on

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 />";
  }
Avatar of Deja Anbu
Deja Anbu
Flag of Oman image

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


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?
ASKER CERTIFIED SOLUTION
Avatar of Scott Madeira
Scott Madeira
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pixelscape
pixelscape

ASKER

perfect... added a double quote to what you wrote and it works fine...

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

thx