CRUD with Angularjs and PHP

Asked

Viewed 620 times

0

Good night!

I got the following:

var carregarUsuario = function () {
    $http.get("buscar.php").then(function (retorno){
        console.log(retorno.data);
        $scope.usuarios = retorno.data;
    });
};
carregarUsuario();

And PHP

    error_reporting(0);
    $user = "root";
    $password = "";
    $db = "angulardb";
    $host = "localhost";
    $con = mysqli_connect("localhost", $user, $password, $db);
    if (mysqli_connect_errno()){
      echo "Erro: " . mysqli_connect_error();
      }
    $usuario = mysqli_query($con, "SELECT *  FROM users");

        header('Content-Type: application/json');
        $return = array();

        while ($dados = mysqli_fetch_assoc($usuario)) {
            array_push($return, $dados);
        }

        echo json_encode($return);

No data is being displayed on the page, or console. What may be?

EDIT

When I put a print_r in my PHP code, right after the while I have:

Array
(
    [0] => Array
        (
            [0] => 2
            [id] => 2
            [1] => João Silva
            [nome] => João Silva
            [2] => [email protected]
            [email] => [email protected]
            [3] => 123456
            [pass] => 123456
        )

    [1] => Array
        (
            [0] => 3
            [id] => 3
            [1] => Mario de Almeida
            [nome] => Mario de Almeida
            [2] => [email protected]
            [email] => [email protected]
            [3] => 123456
            [pass] => 123456
        )

)
  • Look at your console’s returned some error.

  • Your console shows absolutely nothing? What if you only use console.log(retorno) ?

  • @Viniciussilva the island is blank

  • @Using return only, the following is displayed: Object {date: ", status: 200, config: Object, statusText: "OK"}

  • @Lucastorres then make sure that php is processing the data correctly, use some 'print_r' to see what the returns are. Could be a problem in php in data generation.

  • @Celsomtrindade, after the push array_push, still inside the while block, I added print_r($Return); and it returns me the data: Array ( [0] => Array ( [0] => 2 [id] => 2 [1] => João Silva [name] => João Silva [2] => [email protected] [email] => [email protected] [3] => 123456 [pass] => 123456 ) Array ( [0] => Array ( [0] => 2 [id] => 2 [1] => João Silva [name] => João Silva [2] => [email protected] [email] => [email protected] [3] => 123456 [pass] => 123456 ) [1] => Array ( [0] => 3 [id] =>...etc

  • 1

    1- Open your Chrome, 2 - enable developer tools 3 - Go to the networking tab. 4, Look what’s coming from Answer in your php, see if it’s a valid JSON or if it’s coming some Error 500, etc... Basically your problem is in the return of AJAX. I suggest creating a catchof error: $http.get("buscar.php").then(funcao sucesso, funcao catcherro)

Show 2 more comments

2 answers

1

Try using the full resource url and display the return variable in the console.

var carregarUsuario = function () {
    $http.get("http://localhost/buscar.php").then(function (retorno){
        console.log(retorno);
        $scope.usuarios = retorno;
    });
};
carregarUsuario();

0


Solved by changing the database colation to UTF8

Browser other questions tagged

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