Like query returns nothing when you have more than one word in PHP

Asked

Viewed 215 times

1

I’m trying to make a query using like in PHP and when I use a more than one word.
It only works if I just put one word.
In SQL Server the query works error-free.

    <?php

 function listaMatHistOS($conn, $equipamento){
        $HistMatOS = array();
        $where = [];
        $where[] = "cliente.id = (select usuario.idCliente from usuario where usuario.login = '".usuarioLogado()."')";

        if ($equipamento) {
        $where[] = "material.nome like '%{$equipamento}%'";         
        }
        $SQL = "select os.id as IdOS, os.dataHora, material.nome as equipamento, itemMaterial.nSerie, itemMaterial.patrimonio, 
            itemMaterial.rm, os.status 
from os

inner join itemMaterial on
itemMaterial.id = os.id 

inner join material on
material.id = itemMaterial.idMaterial

inner join cliente on
cliente.id = os.idCliente

where " . implode(' AND ', $where) ;
        $resultado = sqlsrv_query($conn, $SQL);
        while ($RelMatOS = sqlsrv_fetch_array($resultado, SQLSRV_FETCH_ASSOC)){;
        array_push($HistMatOS, $RelMatOS);
    }
    echo $SQL;
    return $HistMatOS;
 }

Consulta sem retorno

  • What record did you expect your like find?

  • All the equipment you have Module ECG. In SQL Server return 6 records and in PHP none.

  • That same query of print that you put in the reply returns 6 records if you copy and put in the SQL Studio?

  • http://imgur.com/a/9KFSw Check print, pfv.

  • Tries to change the % for '%%'

  • Didn’t work.,,

  • like the one I put in? with the ' and td?

  • I discovered that when I consult module ecg does not return anything but if I consult dulo ecg returns values. Ie, The problem is in the accent. I have to remove in the input.

  • before sending your $SQL makes a $SQL = utf8_encode($SQL); then

  • @Sorack.

  • I should advise you that using utf8_decode to solve this case will be practically a gambiarra (not to say that utf8_decode is gambiarra, but the use to solve this specific problem), the problem is elsewhere, this answer should guide you http://en.stackoverflow.com/a/43205/3635, although talking about mysql, the logic is the same, you have to fix the encoding of . php, the headers and the database connections. Now if you are using is-8859-1 (or window-1252) together with ajax, and really only utf8_decode. Could confirm?

Show 6 more comments

1 answer

1


Since the word without accentuation works correctly in the search the problem is probably the encoding used that for sqlsrv_queryis ISO-8859-1. So before you run your query do the decode:

$SQL = utf8_decode($SQL);

That will transform your UTF-8 for ISO-8859-1.

Browser other questions tagged

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