KC22 Posted January 25, 2008 Share Posted January 25, 2008 I get an error when running a query that uses a variable. The query is as follows: 'SELECT B.BB_SESSID, PRODUCTS.PR_ProdID, PR_Descrip, B.NUM FROM PRODUCTS INNER JOIN ( SELECT BB_SESSID, SUBSTRING_INDEX(SUBSTRING_INDEX(BB_Basket, \'|\', -1), \'$\', 1)NUM FROM SESSION )B ON PRODUCTS.PR_NUM = B.NUM WHERE B.BB_SESSID = $babble_id'; The error I get is: Unknown column '$babble_id' in 'where clause' Does anyone see my issue?? If I replace the $babble_id variable with the actual id (68) it works fine. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/87785-solved-sql-query-with-variable-error/ Share on other sites More sharing options...
irvieto Posted January 25, 2008 Share Posted January 25, 2008 Hi You should remove the simple quote and replace it by double quote.. Simple quotes do not allow variable interpretation, double quotes does.. Examples. $id=1; $other='lol'; $sql = "select * from my_table where id=$id"; //Forcing Interpretation $sql = "select * from my_table where id=".$id; //Clean concatenation. No interpretation at all $sql = 'select * from my_table where id='.$id; //Same case $sql = "select * from my_table where id='".$id."' and other_field='".$other."' "; /* Using simple quotes to close the string. Result will be : select * from my_table where id='1' and other_field='lol' */ hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/87785-solved-sql-query-with-variable-error/#findComment-449051 Share on other sites More sharing options...
KC22 Posted January 25, 2008 Author Share Posted January 25, 2008 Thanks...that did the trick! Quote Link to comment https://forums.phpfreaks.com/topic/87785-solved-sql-query-with-variable-error/#findComment-449096 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.