是否可以在Angular框架中访问MySQL数据库,或者像其他Javascript一样不安全,我需要发布到PHP页面以data/json从数据库中检索
小智 -3
// Application module
var dbApp = angular.module('dbApp',[]);
dbApp.controller("DbController",['$scope','$http', function($scope,$http){
// Function to get data from the database
getDatafromDatabase();
function getDatafromDatabase(){
// Sending request to php files
$http.post('databaseFiles/yourfile.php').success(function(data){
// Stored the returned data into scope
$scope.dbData = data;
});
}
Run Code Online (Sandbox Code Playgroud)
以及我们要在 angularjs 脚本中调用的 php 文件(yourfile.php)
<?php
// Including database connections
require_once 'database_connections.php';
// mysqli query to fetch all data from database
$query = "SELECT * from [table_name] ORDER BY [table_column]";
$result = mysqli_query($con, $query);
$arr = array();
if(mysqli_num_rows($result) != 0) {
while($row = mysqli_fetch_assoc($result)) {
$arr[] = $row;
}
}
// Return json array containing data from the databasecon
echo $json_info = json_encode($arr);
?>
Run Code Online (Sandbox Code Playgroud)
HTML 文件看起来像这样
<html ng-app="dbApp">
...
<body>
<div ng-controller="DbController">
...
<!-- Table to show date from database -->
<div class="table-responsive">
<table class="table table-hover">
<tr>
<th>Column1</th>
<th>Column2</th>
<th>Column3</th>
<th>Column4</th>
<th>Column5</th>
</tr>
<tr ng-repeat="dbData in dbData">
<td>
<span>{{dbData .Column1}}</span></td>
<td>{{dbData .Column2}}</td>
<td>{{dbData .Column3}}</td>
<td>{{dbData .Column4}}</td>
<td>{{dbData .Column5}}</td>
</tr>
</table>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我希望它能帮助你
| 归档时间: |
|
| 查看次数: |
7317 次 |
| 最近记录: |