How to use LIKE in bindValue?

Asked

Viewed 649 times

5

How to use the operator %, to the bindValue(), in query down below:

$sql = 'SELECT * FROM nfe WHERE (cliente LIKE :cliente OR :cliente_ IS NULL)';
  • It worked @luccasrodrigo?

  • Yes, sorry for the delay (:

1 answer

2


Your SQL would be:

$sql = 'SELECT * FROM nfe WHERE (cliente LIKE :cliente OR cliente IS NULL)';

I might realize there’s a mistake.

Examples:

Starting with the informed term:

$stmt->bindValue(':cliente', $variavel."%", PDO::PARAM_STR);

or

$stmt->bindValue(':cliente', "$variavel%", PDO::PARAM_STR);

Ending with informed term:

$stmt->bindValue(':cliente', "%".$variavel, PDO::PARAM_STR);

or

$stmt->bindValue(':cliente', "%$variavel", PDO::PARAM_STR);

In any part of the term informed:

$stmt->bindValue(':cliente', "%".$variavel."%", PDO::PARAM_STR);

or

$stmt->bindValue(':cliente', "%$variavel%", PDO::PARAM_STR);

References:

  • because my query would be 'SELECT * FROM nfe WHERE (cliente LIKE :cliente OR cliente IS NULL)'; ? @Virgilionovic

  • if put a joint :cliente_ It’s kind of weird that it doesn’t work the way it is, what the purpose of it is?

  • @luccasrodrigo what is the purpose of your code, because, as I said many times is not necessary other times have need, only answers me in your current code was the way you wanted?

  • Yes, only now when you question me, cliente and cliente_ with the same variables in the bind ends up generating redundancy... right?

  • According to @jlHertel, this redundancy exists and is a limitation of PDO, which does not allow having two parameters with the same name.

  • @luccasrodrigo Ok

Show 1 more comment

Browser other questions tagged

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