php - 如何在 Angular 中显示 MySQL 数据
问题描述
我一直在尝试为我的 covid-19 timepass 学习一些 SQL,并且我设法制作了数据,但我现在陷入了困境,我迷失了如何用角度重复从我的 SQL 中获得的表下表中的数据库!,我查看了 W3School 和其他几个网站,但我没有得到它。
PHP
if($connectServer->connect_error){
die("Connection failed :".$connectServer->connection_error);
}
$result = $connectServer->query("SELECT * FROM Users");
$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
if ($outp != "") {$outp .= ",";}
$outp .= '{"ID":"' . $rs["userID"] . '",';
$outp .= '"First Name":"' . $rs["FName"] . '",';
$outp .= '"Last Name":"' . $rs["FName"] . '",';
$outp .= '"City":"' . $rs["City"] . '",';
$outp .= '"Email":"'. $rs["Email"] . '"}';
}
$outp ='{"names":['.$outp.']}';
echo($outp);?>
HTML
<table ng-controller="customersCtrl" ng-app="myApp">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>City</th>
<th>Email</th>
</tr>
<tr ng-repeat="a in names">
<td>{{a.ID}}</td>
<td>{{a.First Name}}</td>
<td>{{a.LName}}</td>
<td>{{a.City}}</td>
<td>{{a.Email}}</td>
</tr>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("php/fetchDATA.php")
.then(function (response) {$scope.names = response.data.records;});
});
</script>
获取数据.PHP
{"names":[{"ID":"32","First Name":"ValueFName","Last Name":"ValueFName","City":"ValueCity","Email":"ValueEmail"}, {"ID":"33","First Name":"ValueFName","Last Name":"ValueFName","City":"ValueCity","Email":"ValueEmail"},{"ID":" 34","First Name":"ValueFName","Last Name":"ValueFName","City":"ValueCity","Email":"ValueEmail"}]}
解决方案
您的每个代码中都有一些错误,例如变量{{a.First Name}}
有空格并且{{a.LName}}
没有在任何地方定义。
因此,您必须更改 php 中的变量声明才能使代码正常工作。
此外,您期望的响应必须具有变量记录,但您定义的 php 没有任何此类变量。因此,我建议您在提问之前再次阅读 PHP 和 Angular 教程。
推荐阅读
- google-earth-engine - 是否有一个函数可以在谷歌地球引擎中给出“ee.Image”的像素大小(不是以米为单位)?
- android - Recyclerview 的项目正在选择重复位置并更改文本颜色
- powershell - 需要有关 powershell 中 where-object 的帮助
- css - 带有 angularjs 的 style="-webkit-mask-image: url()" 中的 HTTP 404
- google-sheets - 如何将此 Google 工作表的内容动态导入新工作表?
- datetime - 如何计算周末、节假日等休息日交易的数量?
- asp.net - ASP.Net 核心 web api 中的数据库查询过滤
- javascript - 从 javascript 到 C# 的函数解码器
- c# - 无法将 Json 对象反序列化为类型
- java - 我可以将一段代码作为参数添加到方法中吗?