Print data from a JSON

Asked

Viewed 1,419 times

2

I have an SQL query that I have already tested and it is bringing the correct result, my difficulty is in creating an array with the line data, turn it into JSON and print on 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";
}

Example array with strings (works)

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

Example array with variables (does not work)

$encode[] = array($row[1], $row[2], $row[3], $row[4]);
  • 1

    $Ncode[] = $Row;

  • Add an example of array de strings and array de variáveis

  • @Edsonhoraciojunior are these: array with variables $encode[] = array( $row[1], $row[2], $row[3], $row[4]); and now with strings $encode[] = array("palavra1", "palavra2", "palavra3"); . The second works the first no.

  • @Victoraraujo this type of information should be on the question because it can help other users, can let me edit here.

  • What is the value of $row[1]?

  • @Edsonhoraciojunior the first $Row[1] is "Administrator", the second is "Health Test", which is in agreement with the bank

Show 1 more comment

1 answer

1


looks like your while

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

after the loop I do the echo thus:

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

This if you need type formatting json by default, if you don’t need to change anything here

EDIT:

As your result in comment below the code is working, maybe you are not only able to use it, I will help you in this part now:

Let’s understand what the var_dump() 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 at position 1 we have the first record with u.id_user, u.nome, u.cargo, u.setor, u.usuario, p.nome to use it to use it in php would be so:

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 Indice to 1 you will have the results of according to register of your select, now how to handle this in javascript? follows below:

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

BONUS:

Particularly I like to work with the concept of objects a simple look at function mysql_fetch_object() would bring 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
});
  • Also, it’s not working =/

  • of the one var_dump($encode) and debug, has msg error, Warnig, notice something ?

  • the result of var_dump is array(2) { [0]=> array(6) { [0]=> string(1) "1" [1]=> string(13) "Administrator" [2]=> string(13) "Administrator" [3]=> string(2) "RH" [4]=> string(5) "admin" [5]=> string(5) "Total" } [1]=> array(6) { [0]=> string(1) "2" [1]=> string(11) "Test Sa de" [2]=> string(7) "Manager" [3]=> string(9) "Marketing" [4]=> string(11) "test-health" [5]=> string(5) "Sa de" } } and no error in php log.

  • Obs: I changed the code to mysqli_fetch_row

  • @Victoraraujo made an issue, with that I think you solve the whole problem

  • Man, thank you so much, I got it here now, I used the hint of Object and Alias, I didn’t know how to do it before, it was even worth!!!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.