2017-06-26 104 views
0

我想使用PHP和AngularJS从MySql数据库读取数据。使用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); 
?> 

我目前的问题是readEmployees : function()函数里面的factory.js没有被调用。 有什么不对?

我在控制台看到这个错误。 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

+0

你看到控制台上的任何错误?此外你的webservice.php似乎不会输出任何东西,例如'echo json_encode($ returnarray)' –

+0

我看到错误,因为webservicefactory没有定义。我有这个文件在同一个文件夹中。 – batuman

+0

你称之为“myApp”而不是“app”。 –

回答

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,并且只接受请求中的数字diggy on php doc if find every事情你需要快乐:) 关于角我认为每件事情都是正确的,当你在PHP上传递正确的数据大多数工作

+0

感谢您的建议。现在我用了PDO。 – batuman