SQLSTATE [42000]: Syntax error or access violation

1

SQL:

"SELECT * FROM produto WHERE ($cliente=null OR cliente LIKE '%".$cliente."%')
AND ($n_cad=NULL
     OR n_cad=$n_cad)
AND ($cod=NULL
     OR id=$cod)
AND ($dt_inicial=NULL
     OR data_emissao >= $dt_inicial)
AND ($dt_final=NULL
     OR data_emissao <= $dt_final)
ORDER BY id DESC LIMIT ".$min.",".$max;

The error:

SQLSTATE[42000]: Syntax error or access violation: 1064 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 '=null OR
n_cad=) AND (=null OR id=) AND (=null OR data_emissao >= ) AND
(2017-02' at line 1
    
asked by anonymous 25.02.2017 / 17:47

1 answer

1

As I already replied in your another question your problem is in the equality of the parameters.

Try changing the equality symbol with the command IS NULL .

Your query would look like this:

$consulta = "SELECT * FROM produto WHERE ($cliente IS null OR cliente LIKE '%".$cliente."%')
AND ($n_cad IS NULL
     OR n_cad=$n_cad)
AND ($cod IS NULL
     OR id=$cod)
AND ($dt_inicial IS NULL
     OR data_emissao >= $dt_inicial)
AND ($dt_final IS NULL
     OR data_emissao <= $dt_final)
ORDER BY id DESC LIMIT ".$min.",".$max;
    
25.02.2017 / 19:41