SQLSTATE[42000]: Syntax error or access Violation

Asked

Viewed 983 times

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 mistake:

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
  • mysql does not accept =null but it accepts campoX IS NULL, but does not fit your context.

1 answer

1


As I have answered in your another question your problem is the equality of parameters.

Try changing the equality symbol by the command IS NULL.

Your consultation would look this way:

$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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.