2017-06-26 01:37
浏览 117


I am trying to read data from MySql database using PHP and AngularJS.

My codes are


<!doctype html>
<html  ng-app = "myModule">
     <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">
  <body ng-controller="myController">
      <table ng-bind="readEmployees()">
              <th >Name</th>
              <th >Gender</th>
              <th >Salary</th>
              <th >City</th>
          <tr ng-repeat="employee in employees">


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.");


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


    $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;    

My current problem is that readEmployees : function() function inside factory.js is not called. What is wrong?

I see this error at console. enter image description here


Now I removed factory.js and get data from Script.js

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

Error is changed to

enter image description here

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • douye4051 2017-06-26 02:57

    first php mysql_connect()is almost deprecated now you most use pdo()


    $db = new PDO('dblib:host=your_hostname;dbname=your_db;$user, $pass); and so if i understand you want find a article by id

    so you have the correct methods todo this staffs not this way goto php documentation and find "bindValue" here : http://php.net/manual/en/pdostatement.bindvalue.php

    reorganize your php staffs even this staff isNumeric u don't need if you use statement of pdo class you can just write PDO::PARAM_INT in your bind and this accept only numbers in request diggy on php doc if find every thing your need to be happy :) about angular i think every thing is all right when you delivery the correct data on php most be work

    解决 无用
    打赏 举报

相关推荐 更多相似问题