Database access with Angular

孤街浪徒 提交于 2021-02-06 12:43:35

问题


Is it possible to access MySQL databases in Angular framework or would that be insecure like other Javascript and I will need to post to a PHP page to retrieve data/json from a database


回答1:


1- Is it possible to access MySQL databases in angular framework?

The question is not angular specific but YES It's possible , since MySQL 5.7 inserting, updating and deleting records in MySQL via HTTP is possible. Something like this http://127.0.0.1:8080/sql/myhttp/SELECT+name_first,+name_last+FROM+names refer here

Hence you can interact with MySQL directly with any HTTP client with out any middle-ware.

By HTTP Client I mean Curl,Wget or any Http library/API of any language (ajax,request,fetch,axios ... for JavaScript/node)

2 - Would that be insecure like other JavaScript?

Again not JavaScript specific, But Yes it's insecure(not recommended) to directly interact with the database from the client. Why? You need to handle database security issues like SQL Injection from the client side (angular in this case). It's is very inconvenient to do that.

I do recommend to always have database access middle-ware (php,node,python ...) than interacting from client side




回答2:


I wouldnt recommend FireBird because it quickly builds up limitations, plus there are alot of drawbacks you cant really see, a good article that goes into detail about this is; Crisp.chat - Why you should never use firebird I would personally recommend using backend PHP to post and pull with to ensure database security and more backend complex logic you might need, Use MySQL for general use like blogs, users, etc but id personally suggest MongoDB for chat features but that is up to you.




回答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;
});
}

And php file which we want to call in angularjs script (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);
?>

HTML file look like this

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

I hope it helps you



来源:https://stackoverflow.com/questions/46644574/database-access-with-angular

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!