<?php
function listaEnderecos( $conexao , $filtro , $ordem , $usuario ) {
$sql = "select enderecos.*, us.email, cidades.nomecidade
from enderecos
inner join usuarios us on enderecos.idusuario = us.id
inner join cidades on cidades.id = enderecos.idcidade
where us.email = {$usuario} " ;
if ( $filtro <> "" ) {
$sql = $sql . " where enderecos.idcidade like '%{$filtro} %'" ;
}
if ( $ordem <> "" ) {
$sql = $sql . " order by {$ordem} " ;
}
echo PHP_EOL. 'Gerada: ' . PHP_EOL. $sql ;
/*
$resultado = mysqli_query($conexao, $sql );
while ($endereco = mysqli_fetch_assoc($resultado)) {
array_push($enderecos, $endereco);
}
return $enderecos;
*/
}
function listaEnderecosCorrigida( $conexao , $filtro , $ordem , $usuario ) {
$sql = "select enderecos.*, us.email, cidades.nomecidade
from enderecos
inner join usuarios us on enderecos.idusuario = us.id
inner join cidades on cidades.id = enderecos.idcidade
where us.email = {$usuario} " ;
if ( $filtro <> "" ) {
$sql = $sql . " AND enderecos.idcidade like '%{$filtro} %'" ;
}
if ( $ordem <> "" ) {
$sql = $sql . " order by {$ordem} " ;
}
echo PHP_EOL. 'Gerada: ' . PHP_EOL. $sql ;
/*
$resultado = mysqli_query($conexao, $sql );
while ($endereco = mysqli_fetch_assoc($resultado)) {
array_push($enderecos, $endereco);
}
return $enderecos;
*/
}
//Caso do erro:
listaEnderecos( null , 'abc' , null , 'usuario teste' ) ;
//Caso onde o erro ficou oculto
listaEnderecos( null , null , null , 'usuario teste' ) ;
//Função corrigida
listaEnderecosCorrigida( 'null' , 'filtro_' , null , 'usuario teste' ) ;
PD9waHAgCgpmdW5jdGlvbiBsaXN0YUVuZGVyZWNvcygkY29uZXhhbywgJGZpbHRybywgJG9yZGVtLCAkdXN1YXJpbykgewoJJGVuZGVyZWNvcyA9IGFycmF5KCk7Cgkkc3FsID0gInNlbGVjdCBlbmRlcmVjb3MuKiwgdXMuZW1haWwsIGNpZGFkZXMubm9tZWNpZGFkZQoJCQlmcm9tIGVuZGVyZWNvcwoJCQlpbm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gdXMuZW1haWwgPSB7JHVzdWFyaW99IjsKCglpZiAoJGZpbHRybyA8PiAiIikgewoJJHNxbCA9ICRzcWwgLiIgQU5EIGVuZGVyZWNvcy5pZGNpZGFkZSBsaWtlICcleyRmaWx0cm99JSciOwoJfQoKCWlmICgkb3JkZW0gPD4gIiIpIHsKCSRzcWwgPSAkc3FsIC4iIG9yZGVyIGJ5IHskb3JkZW19IjsKCX0KCgllY2hvIFBIUF9FT0wuJ0dlcmFkYTogJy5QSFBfRU9MLiAkc3FsOwoJLyoKCSRyZXN1bHRhZG8gPSBteXNxbGlfcXVlcnkoJGNvbmV4YW8sICRzcWwgKTsKCgl3aGlsZSAoJGVuZGVyZWNvID0gbXlzcWxpX2ZldGNoX2Fzc29jKCRyZXN1bHRhZG8pKSB7CglhcnJheV9wdXNoKCRlbmRlcmVjb3MsICRlbmRlcmVjbyk7Cgl9CglyZXR1cm4gJGVuZGVyZWNvczsKCSovCgl9CgoKLy9DYXNvIGRvIGVycm86Cmxpc3RhRW5kZXJlY29zKG51bGwsICdhYmMnLCBudWxsLCAndXN1YXJpbyB0ZXN0ZScpOwoKLy9DYXNvIG9uZGUgbyBlcnJvIGZpY291IG9jdWx0bwpsaXN0YUVuZGVyZWNvcyhudWxsLCBudWxsLCBudWxsLCAndXN1YXJpbyB0ZXN0ZScpOwoKLy9GdW7Dp8OjbyBjb3JyaWdpZGEKbGlzdGFFbmRlcmVjb3NDb3JyaWdpZGEoJ251bGwnLCAnZmlsdHJvXycsIG51bGwsICd1c3VhcmlvIHRlc3RlJyk7
stdout
Gerada:
select enderecos.*, us.email, cidades.nomecidade
from enderecos
inner join usuarios us on enderecos.idusuario = us.id
inner join cidades on cidades.id = enderecos.idcidade
where us.email = usuario teste where enderecos.idcidade like '%abc%'
Gerada:
select enderecos.*, us.email, cidades.nomecidade
from enderecos
inner join usuarios us on enderecos.idusuario = us.id
inner join cidades on cidades.id = enderecos.idcidade
where us.email = usuario teste
Gerada:
select enderecos.*, us.email, cidades.nomecidade
from enderecos
inner join usuarios us on enderecos.idusuario = us.id
inner join cidades on cidades.id = enderecos.idcidade
where us.email = usuario teste AND enderecos.idcidade like '%filtro_%'