nickholt1972 Posted October 7, 2006 Share Posted October 7, 2006 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. Link to comment https://forums.phpfreaks.com/topic/23300-using-a-php-variable-in-an-mysql-query/ Share on other sites More sharing options...
printf Posted October 7, 2006 Share Posted October 7, 2006 Enclose {$var} or dot ' . $var . '...example (1)// enclose method[code]$productdetails = @mysql_query ( 'SELECT * FROM stocks WHERE id = {$_SESSION['cart'][$i]}' );[/code]example (2)// dot method[code]$productdetails = @mysql_query ( 'SELECT * FROM stocks WHERE id = ' . $_SESSION['cart'][$i] );[/code]Notice on the right side of the variable, there is no * [b]. '[/b] *, because it isn't needed when a variable beigins or ends a function(s) param value or a echo and print statement or any $varible assignment!me! Link to comment https://forums.phpfreaks.com/topic/23300-using-a-php-variable-in-an-mysql-query/#findComment-105662 Share on other sites More sharing options...
nickholt1972 Posted October 7, 2006 Author Share Posted October 7, 2006 Thanks man, you have [u]no idea[/u] how long i've been fighting with that particular beast.Nick Link to comment https://forums.phpfreaks.com/topic/23300-using-a-php-variable-in-an-mysql-query/#findComment-105664 Share on other sites More sharing options...
fenway Posted October 9, 2006 Share Posted October 9, 2006 To be clear, PHP can't guess how much of your "complex" variable name is really "there" unless you make it explicit. Link to comment https://forums.phpfreaks.com/topic/23300-using-a-php-variable-in-an-mysql-query/#findComment-106093 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.