使用PHP和AngularJS从MySql数据库读取数据

时间:2017-06-26 01:37:35

标签: javascript php mysql angularjs

我正在尝试使用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()函数。 有什么问题?

我在控制台看到此错误。 enter image description here

编辑:

现在我删除了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;
        });
      }); 

错误更改为

enter image description here

1 个答案:

答案 0 :(得分: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

重新组织你的php员工即使你使用pdo类声明你也不需要这个人员 你可以在你的绑定中写PDO :: PARAM_INT,这只接受php doc中请求diggy中的数字如果找到你需要的所有东西:) 关于角度我认为当你在php上交付正确的数据时,每件事情都是正常的