Undefined in php query

0

I do not know why, but it shows me the data correctly in one query and in the other I get the data Undefined !

Showing the following error when going through the for console.log($scope.servicio):

process
  



Notice : Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/Paneldetencion/app/php/consultaServicios.php on line 9

  Error querying database.

And when I show only the data console.log(data):

  


Notice : Undefined property: stdClass :: $ service_id in /Applications/XAMPP/xamppfiles/htdocs/Paneldetencion/app/php/consultaServicios.php on line 9
  Error querying database.

The two throw me to the same line the 9: $postdata = file_get_contents("php://input"); The php query is this:

<?php  
    header('Access-Control-Allow-Origin: *');    

    date_default_timezone_set("Chile/Continental");    
    $db = mysqli_connect('localhost','root','','mongos')
    or die('Error connecting to MySQL server.');
    $postdata = file_get_contents("php://input");
    $request = json_decode($postdata);
    $id = $request->id;

    $query = "SELECT identificador, nombre FROM Servicios WHERE id =".$id;
    mysqli_query($db, $query) or die('Error querying database.', error_reporting());

    $result = mysqli_query($db, $query);
    $array = array();
    while ($row = mysqli_fetch_array($result)) {
        $array[] = array(
            $row["identificador"]
           $row["nombre"]
        ); 
    }

   echo $json_info = json_encode($arr);

?>

And the controller is like this:

        $http({
        method: 'POST',
        url: 'http://localhost/Paneldetencion/app/php/consultaServicios.php',  
     //   url: 'php/consultaServicios.php',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        },

        data:{

          id: 38
        }

    })

.then(function(data) {

    var dat = data.data;

    $scope.servicio = [];
          for (var i = 0; i < dat.length; i++) {

            var datos = {
                nombre: dat[i].nombre,
                identificador: dat[i].identificador,
            };

            $scope.servicio.push(datos);
        } 

        console.log("RESPUESTA: ",$scope.servicio)
});

As I said before, this same query I have it with other data and returns them correctly, but not all the data by the id where. But he shows them. Here all data is Undefined

    
asked by Hernan Humaña 08.11.2016 в 21:48
source

2 answers

1

The error is that you do

echo $json_info = json_encode($arr);

And $arr is not defined anywhere, it should be with $array

EDIT

For what we saw in the chat the first error (after you name in this answer) is that you send from AngularJS the value servicio_id while in your file php you are looking for id and This was never going to work. And this error can not be displayed in your question since you did not put the actual code where the error was occurring, the code you left works perfectly.

The error was with your database after solving the request http from AngularJS when it returned that one of your fields was not in the table you were looking for. That's another mistake that I think goes in another question.

    
answered by 08.11.2016 / 22:53
source
1

I think this row is too:    $ request = json_decode ($ postdata); Since you are not sending a Json but an Objec.

    
answered by 08.11.2016 в 22:06