duanchu0031 2017-06-26 01:37 采纳率: 0%
浏览 117
已采纳

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

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

My codes are

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

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

EDIT:

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()

    try

    $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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决