Error in a SQL syntax - mysqli_real_escape_string

0

I have a syntax error in my code, but I can not find which one is wrong. This is the error I'm getting:

  

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where categoria =' hydrosanitario '' at line 1

My code:

$teste=mysqli_real_escape_string($conexao,$id);
$sql2 = "SELECT * FROM 'downloads'  order by datacadastro DESC  where 'categoria'= '".$teste."'";

I have already searched in several places and the closest answer to my problem I encountered was to put $teste the way I already put it in the above code.

    
asked by anonymous 17.10.2015 / 05:25

2 answers

2

Follow the block by placing the search terms first, and then you must specify how you want them to appear.

SELECT * FROM tabela_ ORDER BY campo1 ASC

This SQL query can be written in several ways, even though there are more appropriate methods for each situation.

In this first form, simply break the quotation marks, and enter the variable where it should be:

$sql2 = "SELECT * FROM 'downloads' where 'categoria'= '".$teste."' order by datacadastro DESC";

You can also interpose strng by typing the variable inside braces {} :

$sql2 = "SELECT * FROM 'downloads' where 'categoria'= '{$teste}' order by datacadastro DESC";

There are several other ways to write a SQL query.

If the test variable is an (int) integer, I recommend that you write it without the single quotation marks.     

17.10.2015 / 06:09
0

I believe the problem is in select, I think the order by has that is after the where clause:

$sql2 = "SELECT * FROM 'downloads' where 'categoria'= '".$teste."' order by datacadastro DESC";
    
17.10.2015 / 05:49