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