Variable within the WHERE of a select

0

I have a variable that changes its content according to some user actions!

I want to use this same variable inside a select, as follows!

" Select * FROM tabele WHERE '$variável' "

However, when I apply variável to Select it displays the following error:

Fatal error: Call to a member function fetchAll() on boolean

As stated above the contents of this variable is dynamic, its contents can be:

$variavel = "Item1 = 'Dado'";

or:

$variavel ="Item1 = 'Dado' AND Item2 = 'Dado'";

Varies according to user action. However, if I add the contents of the variable in WHERE directly, it works perfectly. Ex:

" Select * FROM tabele WHERE Item1 = 'Dado' AND Item2 = 'Dado'"
  

My question is:

     
    

How do I apply the contents of the variable within select without error? Apparently there have to be single and double quotation marks, but I've already made some changes here and nothing worked!

  
    
asked by anonymous 12.09.2016 / 03:28

1 answer

4

Basically it's syntax error, this does not make sense:

"Select * FROM tabele WHERE '$variável'"

Well, when you do

$variavel ="Item1 = 'Dado' AND Item2 = 'Dado'";

Your query is enclosed in quotation marks

Select * FROM tabele WHERE 'Item1 = 'Dado' AND Item2 = 'Dado''

This type of error can be easily detected with a echo $sql; before sending query . For your case, the line would be this:

"Select * FROM tabele WHERE $variável"

or more elegant, to avoid interpolation in the whole string:

'Select * FROM tabele WHERE '.$variável
    
12.09.2016 / 03:32