0

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

7
  • Are you seeing any errors on the console? Also your webservice.php does not seem to be outputting anything anyways, eg echo json_encode($returnarray) Commented Jun 26, 2017 at 1:51
  • I see the error as webservicefactory is not defined. I have this file in the same folder. Commented Jun 26, 2017 at 1:58
  • You called it myApp instead of app. Commented Jun 26, 2017 at 2:00
  • 1
    Above that it also says app is not defined. in one part you have var myApp in the other you try to use app Commented Jun 26, 2017 at 2:00
  • ic, so changed to app to myApp? Commented Jun 26, 2017 at 2:02

1 Answer 1

1

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

1
  • Thanks for the suggestion. Now I used PDO. Commented Jun 26, 2017 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.