2017-02-24 82 views
1

有人可以告訴我我在這裏缺少什麼代碼來顯示我的數據庫中的數據?非常感激!AngularJS + PHP + MySQL來顯示數據庫中的數據

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

如果您瀏覽到'db_con.php'file是什麼反應,你看到了什麼?您是否在console.log中查找任何錯誤? –

+0

您的PHP代碼無效。句柄'echo($ outp); }'最後是打破你的代碼。 – lin

+0

目前我的屏幕上沒有任何東西,控制檯也沒有顯示任何東西。現在只需查看網絡狀態,現在嘗試調試一段時間。我應該用print($ outp)替換它嗎?)而不是? –

回答

0

我必須解決的問題。請嘗試此代碼對我來說工作很好。在這裏添加新angular.min.js和一些變化添加

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 
 
?>

相關問題