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;
}
What record did you expect your
like
find?– Sorack
All the equipment you have Module ECG. In SQL Server return 6 records and in PHP none.
– fabricio_wm
That same
query
ofprint
that you put in the reply returns 6 records if you copy and put in theSQL Studio
?– Sorack
http://imgur.com/a/9KFSw Check print, pfv.
– fabricio_wm
Tries to change the
%
for'%%'
– Sorack
Didn’t work.,,
– fabricio_wm
like the one I put in? with the
'
and td?– Sorack
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.
– fabricio_wm
before sending your $SQL makes a
$SQL = utf8_encode($SQL);
then– Sorack
@Sorack.
– fabricio_wm
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?
– Guilherme Nascimento