Database access with Angular

后端 未结 3 1316
自闭症患者
自闭症患者 2021-02-10 05:23

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

相关标签:
3条回答
  • 2021-02-10 06:00

    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

    0 讨论(0)
  • 2021-02-10 06:04
    // 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

    0 讨论(0)
  • 2021-02-10 06:15

    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.

    0 讨论(0)
提交回复
热议问题