douba8048 2017-02-24 13:29
浏览 84

AngularJS + PHP + MySQL用于显示数据库中的数据

Can somebody tell me what am I missing here for the code to display data from my database? Much appreciated!

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); 
  }

?>
  • 写回答

1条回答 默认 最新

  • drix47193 2017-06-19 11:26
    关注

    i have solve issue. Please try this code is working g fine for me. here add new angular.min.js and some changes added

    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>
          <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)) {
            $outp[] = $rs;
      }
        echo json_encode($outp);    
    ?>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比