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
https://forums.phpfreaks.com/topic/87785-solved-sql-query-with-variable-error/
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.

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.