繁体   English   中英

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

[英]Reading data from MySql database using PHP and 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()函数未调用。 怎么了?

我在控制台上看到此错误。 在此处输入图片说明

编辑:

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

错误更改为

在此处输入图片说明

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

尝试

$db = new PDO('dblib:host=your_hostname;dbname=your_db;$user, $pass); 所以如果我了解您想按ID查找文章

因此,您有正确的方法来执行此操作,而不是通过这种方式转到php文档并在此处找到“ bindValue”: http ://php.net/manual/zh/pdostatement.bindvalue.php

重新整理您的php员工,即使这个员工是Numeric,如果您使用pdo类的语句,您也不需要,您只需在绑定中编写PDO :: PARAM_INT即可,并且如果发现您需要的所有内容,则仅接受php doc中要求的数字快乐:)关于角度,我认为当您在php上提供正确的数据时,所有事情都可以正常工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM