6

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

4
  • use firebase firebase.google.com Commented Oct 9, 2017 at 10:45
  • Yes it's Possible. Commented Oct 9, 2017 at 10:47
  • 1
    You 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. Commented 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) Commented Apr 3, 2024 at 17:08

4 Answers 4

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

-1

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

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-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

1 Comment

Code seems usable by looking and not testing but I believe the original post was about Angular 2+ not the original AngularJS D:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.