Error passing PHP Array to AngularJS by JSON

2

I am in doubt on a code I am developing, I am passing a query on the MySql database and I need this response to return in JSON for angularJS to understand and write in HTML.

Follow the code:

<?php 

header('Content-Type:text/html;charset=UTF-8');

    include("../class/conexao.class.php");

    $myData = json_decode($_POST['myData'], true);

    $userid = $myData['userid'];

    $sql = "SELECT * FROM 'usuario' WHERE id_usuario = '$userid'";

    $user = $conn->query($sql) or die("[Protocolo] = #8");

    $rowarray = array();

    $rows = array();

    while($linha = $user->fetch_assoc()) {

        $row['id_usuario'] = $linha['id_usuario'];
        $row['user_nome'] = $linha['user_nome'];
        $row['user_cpf'] = $linha['user_cpf'];
        $row['user_cnh'] = $linha['user_cnh'];
        $row['user_rg'] = $linha['user_rg'];
        $row['user_address'] = $linha['user_address'];
        $row['user_bairro'] = $linha['user_bairro'];
        $row['user_contato'] = $linha['user_contato'];
        $row['user_dt_nascimento'] = $linha['user_dt_nascimento'];
        $row['id_cidade'] = $linha['id_cidade'];
        $row['user_nv_acesso'] = $linha['user_nv_acesso'];

        array_push($rows, $row);
    }

    $dados['userupdate'] = $rows;

    array_push($rowarray, $dados);

    echo json_encode($rowarray);

    $conn->close();
?>

When I see the POST DATA in AngularJS returns

  

200 - [object Object]

But when I duplicate the echo this way:

  

echo json_encode ($ rowarray); echo json_encode ($ data);

The Result on the console goes like this:

  

200 -   "user_cdf": "user_cd": "user_cd": "user_cd": "user_cd": "user_cd" ":"Street   66, Quadra K49, Lot   "User_contact": "user_contact": "6294131510", "user_dt_message": "1996-02-05", "id_city": "150140", "user_nv_acesso": "1"}] } "user_cpf": "1234", "user_cnh": "1234", "user_rg": "1234", "user_name" user_address ":" Street   66, Quadra K49, Lot   "User_contact": "user_contact": "6294131510", "user_dt_message": "1996-02-05", "id_city": "150140", "user_nv_acesso": "1"}] }

Consulting 2 times, whoever can help is grateful.

Edit:

asked by anonymous 06.12.2015 / 17:11

2 answers

1

I just removed redundancies and nonsense:

    <?php 

header('Content-Type:text/html;charset=UTF-8');

    include("../class/conexao.class.php");

    $myData = json_decode($_POST['myData'], true);

    $userid = $myData['userid'];

    $sql = "SELECT * FROM 'usuario' WHERE id_usuario = '$userid'";

    $user = $conn->query($sql) or die("[Protocolo] = #8");

    while($linha = $user->fetch_assoc()) {

        $dados['userupdate'][]['id_usuario'] = $linha['id_usuario'];
        $dados['userupdate'][]['user_nome'] = $linha['user_nome'];
        $dados['userupdate'][]['user_cpf'] = $linha['user_cpf'];
        $dados['userupdate'][]['user_cnh'] = $linha['user_cnh'];
        $dados['userupdate'][]['user_rg'] = $linha['user_rg'];
        $dados['userupdate'][]['user_address'] = $linha['user_address'];
        $dados['userupdate'][]['user_bairro'] = $linha['user_bairro'];
        $dados['userupdate'][]['user_contato'] = $linha['user_contato'];
        $dados['userupdate'][]['user_dt_nascimento'] = $linha['user_dt_nascimento'];
        $dados['userupdate'][]['id_cidade'] = $linha['id_cidade'];
        $dados['userupdate'][]['user_nv_acesso'] = $linha['user_nv_acesso'];

    }

    $conn->close();

    echo json_encode($dados); exit;
?>

In this final snippet, I inverted putting $conn->close(); before echo json_encode(blablabla) and added an "exit" to terminate any executions that might occur. Thus, it gives greater assurance that the results in json will be printed without formatting problems.

Actually, the changes I've posted do not change at all. The problem in the original code is the mess with 3 arrays, use of array_push, etc. Unnecessary and very messy.

    
06.12.2015 / 18:13
1

There is an error in the generated JSON. In column 295, the collection is being incorrectly closed:

"user_nv_acesso":"1"}]}]{"userupdate"

When in fact it should be:

"user_nv_acesso":"1"}]},{"userupdate

Complementing with an array end-token:

"id_cidade":"150140","user_nv_acesso":"1"}]}]

You can validate JSON content using online tools, such as link . Your original JSON looks like this:

    
06.12.2015 / 18:14