Print data from a JSON

2

I have a SQL query that I have already tested and it is bringing the correct result, my difficulty is in creating an array with the row data, transforming it into JSON and printing to the screen.

When I try with an array of strings, it works, when I try with an array of variables containing strings, it goes wrong ...

require '../conn/conn.php';

$conn = new Conn();
$conecta = $conn->conecta();

//essa consulta está retornando resultados, eu já testei
$sql = "SELECT u.id_user, u.nome, u.cargo, u.setor, u.usuario,  p.nome 
FROM usuarios as u 
INNER JOIN permissoes as p ON (u.permissao = p.id_permissao)";

$result = mysqli_query($conecta, $sql);

$encode = null;

if (mysqli_num_rows($result)>0){
while($row = mysqli_fetch_array($result)){
    //dessa forma não está dando certo
    $encode[] = array( $row[1], $row[2], $row[3], $row[4]);

 //quando eu tento da forma abaixo dá certo e imprime o json numa boa
 // $encode[] = array("palavra1", "palavra2", "palavra3");
}

$conecta->close();
echo json_encode($encode);

}else{

echo "consulta vazia";
}

String array example (works)

$encode[] = array("palavra1", "palavra2", "palavra3");

Array example with variables (not working)

$encode[] = array($row[1], $row[2], $row[3], $row[4]);
    
asked by anonymous 26.02.2016 / 17:58

1 answer

1

look what your while should be

while($row = mysqli_fetch_array($result)){    
    $encode[] = $row;
}

After the loop I make the echo like this:

echo json_encode( Array('json'=>$encode) ); 

If you do not need to change anything here

EDIT:

As your comment result below the code is working, you may not be able to just use it, I'll help you in that part now:

Let's understand what json has brought us:

array(2) { 
    [0]=> array(6) { 
        [0]=> string(1) "1" 
        [1]=> string(13) "Administrador" 
        [2]=> string(13) "Administrador" 
        [3]=> string(2) "RH" 
        [4]=> string(5) "admin" 
        [5]=> string(5) "Total" 
    } 
    [1]=> array(6) { 
        [0]=> string(1) "2" 
        [1]=> string(11) "Teste Sa?de" 
        [2]=> string(7) "Gerente" 
        [3]=> string(9) "Marketing" 
        [4]=> string(11) "teste-saude" 
        [5]=> string(5) "Sa?de" 
    } 
}

We have an array, and in position 1 we have the first record with var_dump() to use it to use it in php would look like this:

echo $enconde[0][0] // pega o u.id_user
echo $enconde[0][1] // pega o u.nome
echo $enconde[0][2] // pega o u.cargo
echo $enconde[0][3] // pega o u.setor
echo $enconde[0][4] // pega o u.usuario
echo $enconde[0][5] // pega o u.nome

If we change the first index to 1 it will have the results of the second record of your select, now how to tinker with it in javascript? below:

$.ajax({
  url: "test.html",
  context: document.body
}).done(function(json) {
  alert( json[0][0] ); // // pega o u.id_user
});

BONUS:

Particularly I like working with the objects concept. A simple look at the mysql_fetch_object () function would give you results like this:

PHP

while($obj = mysqli_fetch_object($result)){    
    $encode[] = $obj; // enchendo um array com objetos
}

$obj = $encode[0] // pegando o primeiro objeto
echo $obj->user_id // mostra o id
echo $obj->nome // mostra o nome, opa nome do USUARIO ou da PERMISSAO ? eu já iria colocar uma alias no select para desambiguar isso
...
...

javascript:

$.ajax({
  url: "test.html",
  context: document.body
}).done(function(json) {
  var obj = json[0];
  alert( obj.id_user ); // pega o id_user
});
    
26.02.2016 / 18:02