1

necesito hacer una consulta a una base de datos. Pensé que podía integrar php con una consulta simple pero aún así no me deja. ¿Cómo lo hice? aquí les dejo el código:

index.php

<html>
<head>
    <title></title>
</head>
<body>
<div ng-app="myApp" ng-controller="customersCtrl"> 

<table>
  <tr ng-repeat="x in names">
    <td>{{ x.area_name }}</td>
  </tr>
</table>

</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>

<script>

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("services.php")
    .then(function (response) {$scope.names = response.data.records;});
});
</script>


</body>
</html>

y otro archivo llamado services.php

<?php

include('include/_setting.php');

$mysqli = new mysqli($db_server, $db_user, $db_passwd, $db_name);

if(isset($_POST['service'])){
  //UPDATE
  //INSERT
  //DELETE
}

if(isset($_GET['service'])){ 

    if($_GET['service'] == "getArea"){ 
            $query = "SELECT * FROM ayudantee.area";
            $ret = $mysqli->query($query)->fetch_all(MYSQLI_ASSOC);
              die(json_encode($ret,JSON_UNESCAPED_UNICODE));  
    }

}

function getArea(){
    $ret = array();
    try {
        $query = "SELECT area_name FROM ayudantee.area";
        $ret = $GLOBALS['mysqli']->query($query)->fetch_all(MYSQLI_ASSOC);
    }catch (Exception $err){
        var_dump($err);
    }
    return $ret;
}

?>
2
  • 1
    Y cual es tu error?
    – vdjkelly
    el 19 nov. 2016 a las 18:11
  • @vdjkelly, solo consulta SQL no muestra tabla. el 19 nov. 2016 a las 18:24

2 respuestas 2

1

Parece que el error es que no estas enviando la variable service. Intenta hacer esto:

$http.get("services.php", {
  data: {
    service: 'getArea'
  }
})
.then(function (response) {
  $scope.names = response.data.records;
});
4
  • no nada, no consulta. :( el 19 nov. 2016 a las 22:11
  • Podrías agregar en tu pregunta la respuesta que recibes de services.php?
    – Marcos
    el 19 nov. 2016 a las 23:50
  • Sii eso, como hace? el 20 nov. 2016 a las 13:00
  • Justo debajo de tu respuesta tenes un botón que dice editar
    – Marcos
    el 20 nov. 2016 a las 15:21
0

Primero, buscas el parametro service que venga por parametro, NO LO ENVIAS

if (isset($_GET['service'])) {

    if ($_GET['service'] == "getArea") {
        $query = "SELECT * FROM ayudantee.area";
        $ret = $mysqli - > query($query) - > fetch_all(MYSQLI_ASSOC);
        die(json_encode($ret, JSON_UNESCAPED_UNICODE));
    }

}

Segundo , si es que lo envias, como en la respuesta de @MarcosGallardo, tampoco va a hacer nada porque no retornas nada. Pon

if ($_GET['service'] == "getArea") {
    $query = "SELECT * FROM ayudantee.area";
    $ret = $mysqli - > query($query) - > fetch_all(MYSQLI_ASSOC);
        die(json_encode($ret, JSON_UNESCAPED_UNICODE));
    echo $ret;
}

Tercero, si es que NO tienes que enviarlo, llama a la funcion getArea()

if(isset($_GET['service'])){ 

    if($_GET['service'] == "getArea"){ 
            $query = "SELECT * FROM ayudantee.area";
            $ret = $mysqli->query($query)->fetch_all(MYSQLI_ASSOC);
              die(json_encode($ret,JSON_UNESCAPED_UNICODE));  
    }

}else{
    echo getArea();
}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.