【问题标题】:Reading data from MySql database using PHP and AngularJS使用 PHP 和 AngularJS 从 MySql 数据库中读取数据
【发布时间】:2017-06-26 01:37:34
【问题描述】:

我正在尝试使用 PHP 和 AngularJS 从 MySql 数据库中读取数据。

我的代码是

index.html

<!doctype html>
<html  ng-app = "myModule">
  <head>
     <script src="../bower_components/angular/angular.min.js"></script>
     <script src="Script.js"></script>
     <script src="factory.js"></script>
     <link href="Styles.css" rel="stylesheet">
  </head>
  <body ng-controller="myController">
      <table ng-bind="readEmployees()">
        <thead>
           <tr>
              <th >Name</th>
              <th >Gender</th>
              <th >Salary</th>
              <th >City</th>
           </tr>
        </thead>
        <tbody>
          <tr ng-repeat="employee in employees">
             <td>{{employee.name}}</td>
             <td>{{employee.gender}}</td>
             <td>{{employee.salary}}</td>
             <td>{{employee.city}}</td>
          </tr>
        </tbody>
      </table>
  </body>
</html>

Script.js

var myApp = angular.module("myModule",[])
                   .controller("myController", function($scope, $http){                                                 
                        $scope.readEmployees = function(){                                              
                            webservicefactory.readEmployees().then(function successCallback(response){
                                $scope.employees = response.data.records;
                                $scope.showToast("Read success.");
                            }, function errorCallback(response){
                                $scope.showToast("Unable to read record.");
                            });                  
                        }                   
                    });

factory.js

app.factory("webservicefactory", function($http){ 
    var factory = {
        readEmployees : function(){    
            return $http({
                method: 'GET',
                url: 'http://localhost/AngularJS/webservice/webservice.php'
            });
        }
    };
    return factory;
});

werservice.php

<?php
    $file = "test.txt";
    $fh = fopen($file, 'w') or die("can't open file");  
    $con = mysql_connect("localhost","root","xxxxxxx");

    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("Employees", $con);

    $article_id = $_GET['id'];

    if( ! is_numeric($article_id) )
      die('invalid article id');

    $query = "SELECT * FROM tblEmployees";

    $returns = mysql_query($query);
    // Please remember that  mysql_fetch_array has been deprecated in earlier
    // versions of PHP.  As of PHP 7.0, it has been replaced with mysqli_fetch_array.  
    $returnarray = array();

    while($return = mysql_fetch_array($returns, MYSQL_ASSOC))
    {
      $returnarray[] = $return;    
    }
    fclose($fh);
    mysql_close($con);
?>

我目前的问题是 factory.js 中的 readEmployees : function() 函数没有被调用。 怎么了?

我在控制台看到这个错误。

编辑:

现在我删除了 factory.js 并从 Script.js 获取数据

var myApp = angular.module('myModule', []);
      myApp.controller('myController', function ($scope, $http){
        $http.get('webservice.php').success(function(data) {
          $scope.employees = data;
        });
      }); 

错误改为

【问题讨论】:

  • 您在控制台上看到任何错误吗?此外,您的 webservice.php 似乎也没有输出任何东西,例如echo json_encode($returnarray)
  • 我看到了错误,因为 webservicefactory 没有定义。我在同一个文件夹中有这个文件。
  • 你叫它myApp而不是app
  • 上面还说应用未定义。一方面你有var myApp,另一方面你尝试使用app
  • ic,所以改成app到myApp?

标签: javascript php mysql angularjs


【解决方案1】:

第一个 php mysql_connect() 几乎已被弃用,现在你最常使用 pdo()

试试

$db = new PDO('dblib:host=your_hostname;dbname=your_db;$user, $pass); 所以如果我知道你想通过 id 找到一篇文章

所以你有正确的方法来做这个工作人员而不是这样 转到 php 文档并找到“bindValue” 这里:http://php.net/manual/en/pdostatement.bindvalue.php

如果你使用 pdo 类的语句,即使这个人员是数字,你也不需要重新组织你的 php 人员 你可以在你的绑定中写 PDO::PARAM_INT ,如果找到你需要快乐的每一件事,这只接受 php doc 上的请求 diggy 中的数字 :) 关于角度,我认为当您在 php 上提供正确的数据时,一切都很好

【讨论】:

  • 感谢您的建议。现在我使用了 PDO。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-31
  • 2018-07-07
  • 2018-04-21
相关资源
最近更新 更多