Jump to content

[SOLVED] Sql query with variable error


KC22

Recommended Posts

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

Link to comment
Share on other sites

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.

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.