0
Hello.
I’m making some queries to the database that uses the PDO.
When I make the appointment without the %
at the beginning of the condition, the results return correctly.
Ex.:
$localizacoes = $localizacao->find("nome LIKE :nome", "nome={$consultaLocalizacao}%")->order('nome')->limit($paginator->limit())->offset($paginator->offset())->fetch(true);
However, when I just add the %
at the beginning of the condition, in some searched words return error.
Ex.:
$localizacoes = $localizacao->find("nome LIKE :nome", "nome=%{$consultaLocalizacao}%")->order('nome')->limit($paginator->limit())->offset($paginator->offset())->fetch(true);
Error:
Fatal error: Uncaught PDOException: SQLSTATE[22021]: Character not in repertoire: 7 ERROR: invalid byte sequence for encoding "UTF8": 0xba in /opt/lampp/htdocs/sioepsul/vendor/coffeecode/datalayer/src/DataLayer.php on line 224
This happens when I search for words starting with: ba, be, ca, ce, fa, fe
, among others. Modifies only the byte Quence to: 0xba, 0xbe, 0xca 0x25, 0xce 0x25, 0xfa, 0xfe
. The Datalayer (Coffeecode) component only does the execute()
from the PDO, I don’t believe it’s a problem in it.
How can I solve this problem?
The problem is that the value is being passed literally. It needs to escape %in the definition of the pair. It would be as if %:
find("nome LIKE CONCAT('%',:nome,'%')
but it only masks another problem. Whenever you post, instead of your original snippet, provide a [mcve] of the problem to detect the source of the problem. To better enjoy the site worth reading What is the Stack Overflow and the Stack Overflow Survival Guide (summarized) in Portuguese.– Bacco
The problem was solved using solution 1 presented by @Duardo-bissi
– Rafael Lopes
I’m glad you solved it. Anyway, see the links passed to facilitate a faster feedback in the next posts, understand and better enjoy the philosophy of the site. And beware of third-party libs, they often generate more problem than medium-term solution.
– Bacco