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
-
use firebase firebase.google.comSachila Ranawaka– Sachila Ranawaka2017-10-09 10:45:51 +00:00Commented Oct 9, 2017 at 10:45
-
Yes it's Possible.Feras Al Sous– Feras Al Sous2017-10-09 10:47:41 +00:00Commented Oct 9, 2017 at 10:47
-
1You can't access MySQL from Angular directly. You need a server that provides a REST API (or WebSocket, ...) that processes requests from Angular, forwards them to the database and returns the result to the Angular application. The suggested Firebase provides such an API as do many others for all kinds of databases.Günter Zöchbauer– Günter Zöchbauer2017-10-09 10:53:38 +00:00Commented Oct 9, 2017 at 10:53
-
I actually find this question kind of broad and to be up voted 5 times is a bit surprising, since does not fallow the guidelines of the site (does not really show basic research)Mauricio Gracia Gutierrez– Mauricio Gracia Gutierrez2024-04-03 17:08:31 +00:00Commented Apr 3, 2024 at 17:08
4 Answers
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.
Comments
As others have mentioned accessing directly from Angular to any database is not a good practice.
Here you can see a detail guide about connection Angular with Mysql using express (REST/API)
https://developer.okta.com/blog/2019/08/16/angular-mysql-express
Comments
Angular is a client side rendering framework that why angular not support database connectivity.you have to use a rest api.use express js for developing a rest api. express js and angular uses a same language that why it is easy for you
1 Comment
// 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