getJSON PHP does not show result

0

I'm doing a simple query via getJSON, but it's not returning the values to be shown.

File testJson.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento sem título</title>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script><script>$(document).ready(function(){//$("#email").click(function() {

        nome = "listar";
        $.getJSON("testeConexaoMobileJson.php", {acao:nome}, function(json){
            $("#nome").val(json[0].nome);
            $("#email").val(json[0].email);
        });
    //});
});
</script>
</head>

<body>
<h1>Cardápio de Pizzas</h1>
    <div id="email"></div>
    <input type="text" name="nome" id="nome"> 

</body>
</html>

File testConexaoMobileJson.php

<?php
$mysqli = new mysqli('localhost','','',''); if($mysqli->connect_error){echo "Erro";exit();}

if($_GET['acao'] == 'listar'){
    header('Content-Type: application/json; charset=utf-8'); 
    $rs = $mysqli->query("SELECT * FROM cadastro");
    $row_rs = $rs->fetch_assoc();
    $row = $rs->fetch_array();  
    $registros = mysqli_num_rows($rs);

    while($campos=mysqli_fetch_array($rs)) { 
        extract($campos); 
        $Array = Array(); 

        $Array[] = Array(
                            "nome"  => "$nome",
                            "email" => "$email",
                        ); 

        $json_encode = json_encode($Array); 
        echo $json_encode; 
    } 


}
?>

Result of testConexaoMobileJson.php file

Inspector :

Result:

  

[

    
asked by anonymous 10.12.2015 / 12:55

3 answers

4

Your code is very redundant, basically what you need is this:

$rs = $mysqli->query("SELECT * FROM cadastro");

$registros = $rs->num_rows;

$arr = array();
while($row = $rs->fetch_assoc()) { 
    $arr[] = $row;
} 

$json_encode = json_encode($arr); 
echo $json_encode; 

Problems:

$row_rs gets the first record of the query in an associative array, $row does the same thing except it receives an array indexed by numbers and an associative. Tip delete the line from the $row assignment.

$rs = $mysqli->query("SELECT * FROM cadastro");
$row_rs = $rs->fetch_assoc();
$row = $rs->fetch_array();  


 while($campos=mysqli_fetch_array($rs)) { 
    extract($campos); 
    $Array = Array(); 
    $Array[] = Array("nome"  => "$nome", "email" => "$email",); 
    $json_encode = json_encode($Array); 
    echo $json_encode; 
} 

It does not make much sense to use extract() delete it. At every turn of while $array is reset with an empty array, the next line already has a syntax error, and a comma is left there at the end.

Array conversion to json should only be done after while since all values are converted at once.

If you only want some fields of the tables, not all of them, change the asterisk name to the fields.

Change:

SELECT * FROM cadastro

To:

SELECT nome, email FROM cadastro
    
10.12.2015 / 13:06
3

You can only display data with json_encode out of while , my friend.

while ($campos = mysqli_fetch_array($rs)) { 

    extract($campos); 

    $Array[] = Array(
        "nome"  => "$nome",
        "email" => "$email",
    ); 


    //  Melhor assim

    $Array[] = $campos;


} 

echo json_encode($Array);
    
10.12.2015 / 13:05
-1

It may be that the browser is going to get some output after the echo. Try closing PHP as echo json_encode( $resultado); exit(); . In javascript, you can use the following code:

$.post ( url, data, function( response ) {

    // Abra o console do navegador para visualizar esta saída
    console.log( response );

    // Digite seu código aqui

}, 'json' );

// Observe que você pode passar o parâmetro 'json' no final
// para converter a resposta ao invés de usar uma função específica
    
10.12.2015 / 14:13