2017-02-24 1 views
1

Quelqu'un peut-il me dire ce qui me manque ici pour que le code affiche les données de ma base de données? Très appréciée!AngularJS + PHP + MySQL pour afficher les données de la base de données

HTML

<!DOCTYPE html> 
    <html lang="en" ng-app="VinylApp"> 
    <head> 
     <meta charset="utf-8">  
     <title>Vinyl Record Store</title> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script> 
     <script src="script.js"></script><script src="app.js"></script> 
     <link rel="stylesheet" href="main.css"> 
    </head> 

    <body> 
    <div ng-app="VinyApp" ng-controller="VinylListController"> 

     <table> 
     <tr ng-repeat="vinyl in vinyls"> 
      <td>{{vinyl.Vinyl_ID}}</td> 
      <td>{{vinyl.VinylName}}</td> 
      <td>{{vinyl.Artist}}</td> 
      <td>{{vinyl.Price}}</td> 
     </tr> 
     </table> 
    </div> 
    </body> 
</html> 

JS

var app= angular.module('VinylApp', []); 
app.controller('VinylListController', function($scope, $http){ 
    $http.get("db_con.php") 
    .then(function(response){ 
    $scope.vinyls = response.data.records;  
    }); 
}); 

PHP

<?php 
    header("Access-Control-Allow-Origin: *"); 
    header("Content-Type:application/json; charset=UTF-8"); 

    $conn = new mysqli("myServer","myUser", "myPassword", "Northwind"); 
    $result = $conn->query("SELECT * FROM vinyl"); 
    $outp= ""; 
    while($rs=$result->fetch_array(MYSQLI_ASSOC)){ 
    if ($outp != "") {$outp .= ",";} 
    $outp .= '{"VinylID":"' . $rs["VinylID"] . '",'; 
    $outp .= '"VinylName":"' . $rs["VinylName"]  . '",'; 
    $outp .= '"Artist":"'. $rs["Artist"]  . '",'; 
    $outp .= '"Price":"'. $rs["Price"]  . '"}'; } $outp ='{"records":['.$outp.']}'; $conn->close(); 

    echo($outp); 
    } 

?> 
+0

Si vous accédez à la 'db_con.php'file quelle est la réponse que vous voyez? Avez-vous cheché le console.log pour des erreurs? –

+0

Votre code PHP n'est pas valide. Le guidon 'echo ($ outp); } 'et la fin est la rupture de votre code. – lin

+0

Actuellement, je n'ai rien sur mon écran et la console n'en montre aucune. Il suffit de regarder l'état du réseau maintenant, en essayant de le déboguer pendant un certain temps maintenant. Devrais-je le remplacer par print ($ outp);} à la place? –

Répondre

0

j'ai résoudre la question. S'il vous plaît essayez ce code fonctionne très bien pour moi. ici d'ajouter de nouveaux angular.min.js et quelques changements ajoutés

var app= angular.module('VinylApp', []); 
 
app.controller('VinylListController', function($scope, $http){ 
 
    $http.get("db_con.php") 
 
    .then(function(response){ 
 
    $scope.vinyls = response.data;  
 
    }); 
 
});
<!DOCTYPE html> 
 
    <html lang="en" ng-app="VinylApp"> 
 
    <head> 
 
     <meta charset="utf-8">  
 
     <title>Vinyl Record Store</title> 
 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 
 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.24/angular.min.js"></script> 
 
\t <script src="app.js"></script> 
 
     <script src="script.js"></script> 
 
     
 
    </head> 
 
    <body ng-app="VinyApp"> 
 
    <div ng-controller="VinylListController"> 
 
     <table> 
 
     <tr ng-repeat="vinyl in vinyls"> 
 
      <td>{{vinyl.Vinyl_ID}}</td> 
 
      <td>{{vinyl.VinylName}}</td> 
 
      <td>{{vinyl.Artist}}</td> 
 
      <td>{{vinyl.Price}}</td> 
 
     </tr> 
 
     </table> 
 
    </div> 
 
    </body> 
 
</html> 
 
    
 
    
 
<?php 
 
    
 
    $conn = new mysqli("localhost","root", "", "pinakin_northwind"); 
 
    $result = $conn->query("SELECT * FROM vinyl"); 
 
    $outp = array(); 
 
    while($rs = $result->fetch_array(MYSQLI_ASSOC)) { 
 
\t \t $outp[] = $rs; 
 
    } 
 
\t echo json_encode($outp); \t 
 
?>