简体   繁体   中英

Reading data from MySql database using PHP and AngularJS

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. 在此处输入图片说明

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

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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