nickholt1972 Posted October 7, 2006 Share Posted October 7, 2006 I've posted this in the MySQL forum, but it could be in either and this one has more traffic.I want to select items from my database where the id for the record is equal to the PHP variable being passed from the previous page. In theory the code should look something like...[b]$productdetails = @mysql_query('SELECT * FROM stocks WHERE id = $_SESSION['cart'][$i]');[/b]But I don't seem to be able to use a variable in a query.Does anyone know of an alternative method for getting round this problem?Thanks,Nick Quote Link to comment Share on other sites More sharing options...
Orio Posted October 7, 2006 Share Posted October 7, 2006 You should add single qoutes around it:SELECT * FROM stocks WHERE id = '$_SESSION['cart'][$i]'Orio. Quote Link to comment Share on other sites More sharing options...
nickholt1972 Posted October 7, 2006 Author Share Posted October 7, 2006 That's great, thanks. only problem is, I can't surround the variable with single quotes because i've got singles at the start and end of the SELECT statement. I can't change those to doubles or use doubles to surround the variable because i get error messages. And i can't escape them either.I appreciate your help. Quote Link to comment Share on other sites More sharing options...
shiny_spoon Posted October 7, 2006 Share Posted October 7, 2006 Try this:[code=php:0]'SELECT * FROM stocks WHERE id = '.$_SESSION['cart'][$i].' '[/code]or[code=php:0]'SELECT * FROM stocks WHERE id = "'.$_SESSION['cart'][$i].'" '[/code]Edit: For the record, I'm quite sure that to use a variable directly in a query without concating, you would have to declare it like so: [code=php:0]$var = $_SESSION['cart'][$i];[/code] and then use the variable in double quotes: [code=php:0]'SELECT * FROM stocks WHERE id = "$var" '[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.