[英]Reading data from MySql database using PHP and AngularJS
I am trying to read data from MySql database using PHP and AngularJS. 我正在尝试使用PHP和AngularJS从MySql数据库读取数据。
My codes are 我的密码是
index.html 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 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 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 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. 我当前的问题是factory.js内部的readEmployees : function()
函数未调用。 What is wrong? 怎么了?
I see this error at console. 我在控制台上看到此错误。
EDIT: 编辑:
Now I removed factory.js and get data from Script.js 现在我删除了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;
});
});
Error is changed to 错误更改为
first php mysql_connect()is almost deprecated now you most use pdo() 第一个php mysql_connect()几乎已弃用,现在您最常使用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 所以如果我了解您想按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 因此,您有正确的方法来执行此操作,而不是通过这种方式转到php文档并在此处找到“ bindValue”: http ://php.net/manual/zh/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 重新整理您的php员工,即使这个员工是Numeric,如果您使用pdo类的语句,您也不需要,您只需在绑定中编写PDO :: PARAM_INT即可,并且如果发现您需要的所有内容,则仅接受php doc中要求的数字快乐:)关于角度,我认为当您在php上提供正确的数据时,所有事情都可以正常工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.