0

i'm trying to list the elements of a MySQL database using AngularJS. Despite some research, i can't find what to write in the controller function to do that.

To be more precise, i previously did it without AngularJS using the following :

$link = new PDO('mysql:host=localhost;dbname=mygaloo;charset=utf8', 'root', ''); 
$query = $link->query("SELECT * FROM ".$choice." WHERE nom LIKE '%".$request."%' ORDER BY id DESC"); 

Here's my current code :

<div ng-app="searchApp" ng-controller="searchController"> 
    <table>
        <tr ng-repeat="x in objects">
            <td>{{ x.name }}</td>
        </tr>
    </table>
</div>

<script>
    var app = angular.module('searchApp', []);
    app.controller('searchController', function($scope) 
    {
        //What should i do here ?
    });
</script>
4
  • AngularJS is a Single Page Application framework: en.wikipedia.org/wiki/Single-page_application ... it is common to read the data with ajax... so in your controller you should use $http docs.angularjs.org/api/ng/service/$http Commented Mar 24, 2016 at 9:46
  • Ok, but how do i perform a search in a SQL database with $http? Can you give an exemple? Commented Mar 24, 2016 at 10:21
  • if you split your architecture in server and client, the server (php) will do the database query and generate json as a response to a AJAX request from the client (angularJS) Commented Mar 24, 2016 at 10:27
  • I don't know the server side technology you are using but @Meiko is correct. Angularjs is in the client side, and the server should be responsible on retrieving the data from SQL. Commented Mar 24, 2016 at 10:40

1 Answer 1

3

I'm not sure this will work. Because it shouldn't.

There is a distinct difference between client-side and server-side programming.
Angular and jQuery are run client-side, or, on the clients computer. This means, when you do something like this:

$link = new PDO('mysql:host=localhost;dbname=mygaloo;charset=utf8', 'root', ''); 

All the data needed to browse your database is given to the user. Hell, you provided the root. This means that anyone can simply log on to your database as root, and modify everything in your database, or simply collecting all userdata.

To secure this you need to do some server-side scripting, and an API to expose data to the application.

Server-side code is never exposed to the client, this makes it a good place to store passwords and make connections to your databases. There are many possibilities for client-side scripting.

  • php
  • nodeJS (javascript on the server)
  • ruby

Just pick one, and start reading up on how to use them.

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

Comments

Your Answer

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