0
Hello! I have a script that at the end of each while loop prints a json
echo json_encode($retorno);
The result when running the script is (example bringing several results)
    {
    "IdClienteLS": 1695,
    "Nome": "Magia das Flores",
    "Anuncio": "",
    "Telefones": [{
        "Wpp": 0,
        "Fone": "(49) 3224-0196"
    }],
    "Email": "",
    "RedeSocial": "",
    "Site": "",
    "PalavrasChave": ["", null],
    "Ramos": ["Floriculturas"],
    "Logradouro": "",
    "Bairro": "",
    "Cidade": "Lages",
    "Cep": "",
    "Estado": "SC",
    "Complemento": "",
    "Latitude": "",
    "Longitude": ""
} {
    "IdClienteLS": 20141,
    "Nome": "BF Motos",
    "Anuncio": "",
    "Telefones": [{
        "Wpp": 0,
        "Fone": "(49) 3225-5497"
    }],
    "Email": "",
    "RedeSocial": "",
    "Site": "",
    "PalavrasChave": ["", null],
    "Ramos": ["Motos"],
    "Logradouro": "",
    "Bairro": "",
    "Cidade": "Lages",
    "Cep": "",
    "Estado": "SC",
    "Complemento": "",
    "Latitude": "",
    "Longitude": ""
} {
    "IdClienteLS": 3656,
    "Nome": "Trateq",
    "Anuncio": "",
    "Telefones": [],
    "Email": "",
    "RedeSocial": "",
    "Site": "",
    "PalavrasChave": ["", null],
    "Ramos": ["Motos"],
    "Logradouro": "",
    "Bairro": "",
    "Cidade": "",
    "Cep": "",
    "Estado": "",
    "Complemento": "",
    "Latitude": "",
    "Longitude": ""
} {
    "IdClienteLS": 10874,
    "Nome": "Jeane Karine Marcon - Consultora de Beleza",
    "Anuncio": "",
    "Telefones": [],
    "Email": "",
    "RedeSocial": "",
    "Site": "",
    "PalavrasChave": ["", null],
    "Ramos": ["Motos"],
    "Logradouro": "",
    "Bairro": "",
    "Cidade": "",
    "Cep": "",
    "Estado": "",
    "Complemento": "",
    "Latitude": "",
    "Longitude": ""
} {
    "IdClienteLS": 25713,
    "Nome": "Dalia Festas e Eventos",
    "Anuncio": "",
    "Telefones": [],
    "Email": "",
    "RedeSocial": "",
    "Site": "",
    "PalavrasChave": ["", null],
    "Ramos": ["Eventos", "Festas"],
    "Logradouro": "",
    "Bairro": "",
    "Cidade": "",
    "Cep": "",
    "Estado": "",
    "Complemento": "",
    "Latitude": "",
    "Longitude": ""
}
The complete result is not a valid JSON (tested on Jsonlint). This is because it is the printing of several, one followed by the other. To correct, I tried to mount another array like this:
$retornoScript[] = $retorno;
And after finishing the while loop I found the $returnScript array containing all the others, like this:
echo json_encode($retornoScript);
But the script does not run anything. Neither error nor information. Can anyone tell me what I’m doing wrong? The script was requested by a third party, so the purpose is this: return (after reading some parameters) database information to a JSON.
PHP code for verification:
$buscaClientes = mysqli_query($conn, "SELECT * FROM clientes WHERE status = 1 AND data_alteracao BETWEEN date('{$dataSQL}') AND date('{$dataHJ}') LIMIT {$inicio}, {$fim}");
if(mysqli_num_rows($buscaClientes) != 0){
        //$retornoScript = array();
        while($dadosCliente = mysqli_fetch_array($buscaClientes)){
            //Busca Atividades
            //Verifica se vem da integração ou da importacao
            if($dadosCliente['integracao'] == 1){
                //Busca Atividades
                $sqlAtividadesCliente = mysqli_query($conn, "SELECT cc.id_cliente, cat.nome AS nome_categoria, cat.url FROM categorias cat INNER JOIN cliente_categorias cc ON cat.id_sistema_ls = cc.id_categoria AND cat.status = 1 INNER JOIN clientes cli ON cli.id_ls = cc.id_cliente AND cli.status = 1 AND cli.id_ls = {$dadosCliente['id_ls']} GROUP BY nome_categoria");
            }else{
                //Busca Atividades
                $sqlAtividadesCliente = mysqli_query($conn, "SELECT cc.id_cliente, cat.nome AS nome_categoria, cat.url FROM categorias cat INNER JOIN cliente_categorias cc ON cat.id_ls = cc.id_categoria AND cat.status = 1 INNER JOIN clientes cli ON cli.id_ls = cc.id_cliente AND cli.status = 1 AND cli.id_ls = {$dadosCliente['id_ls']} GROUP BY nome_categoria");
            }
            $countAtividades = mysqli_num_rows($sqlAtividadesCliente);
            //Endereços do Cliente
            $sqlEnderecosCliente = mysqli_query($conn, "SELECT * FROM endereco_cliente WHERE id_cliente = {$dadosCliente['id']}");
            //Telefones do Cliente
            $sqlTelefonesCliente   = mysqli_query($conn, "SELECT * FROM telefone_cliente WHERE id_cliente = {$dadosCliente['id']}");
            //Endereço Mapa
            $sqlEndereco = mysqli_query($conn, "SELECT * FROM endereco_cliente WHERE id_cliente = {$dadosCliente['id']} ORDER BY id_ls ASC LIMIT 1");
            $dadosEndereco = mysqli_fetch_array($sqlEndereco);
            //PalavrasChave
            $palavras    = $dadosCliente['palavras_chave'];
            $palavra     = explode(",", $palavras);
            $numPalavras = count($palavra);
            //Palavras Chave para o Google
            $palavrasChave = array();
            for ($i = 0; $i <= $numPalavras; $i++) {
                $palavrasChave[] = $palavra[$i];
            }
            $telefones = array();
            while($dadosFone = mysqli_fetch_array($sqlTelefonesCliente)){
                    $telefones[] = array("Wpp" => (int) $dadosFone['whatsapp'], "Fone" => $dadosFone['telefone']);
            }
            //Atividades
            if($countAtividades != 0){
                    $ramos = array();
                    while($dadosAtividade = mysqli_fetch_array($sqlAtividadesCliente)){
                        $ramos[] = $dadosAtividade['nome_categoria'];
                    }
            }
            //Retorna Array jSON
            $retorno = array("IdClienteLS"    => (int) $dadosCliente['id_ls'], 
                            "Nome"        => $dadosCliente['nome'], 
                            "Anuncio"     => $dadosCliente['imagem'],
                            "Telefones"   => $telefones,
                            "Email"       => $dadosCliente['email'],
                            "RedeSocial"  => $dadosCliente['redesocial'],
                            "Site"        => $dadosCliente['site'],
                            "PalavrasChave" => $palavrasChave,
                            "Ramos"       => $ramos,
                            "Logradouro"  => trim($dadosEndereco['logradouro']),
                            "Bairro"      => trim($dadosEndereco['bairro']),
                            "Cidade"      => trim($dadosEndereco['cidade']),
                            "Cep"         => trim($dadosEndereco['cep']),
                            "Estado"      => trim($dadosEndereco['estado']),
                            "Complemento" => trim($dadosEndereco['complemento']),
                            "Latitude"    => $dadosCliente['longitude'],
                            "Longitude"   => $dadosCliente['altitude']
                            );
                    //$retornoScript[] = $retorno;
                    echo json_encode($retorno);
                }
}
						
At first, what you did should work, but you’d better [Edit] the question and add the PHP code to state exactly what you’re weaving.
– Woss
The correct is
$retornoScript[] = $retorno;, but it is necessary to know how this data is being processed, only then we will know why the script presents nothing.– Valdeir Psr
@Andersoncarloswoss Thank you, Anderson. I added :)
– Aryana Valcanaia
@Valdeirpsr Thanks, Valdeir. I added :)
– Aryana Valcanaia
Already tried json_encode($return, JSON_FORCE_OBJECT); ?
– Christian Luã Lemos
Hi @Christianluãlemos! Thanks for contributing, but it also doesn’t work.
– Aryana Valcanaia