AngularJS SQL

AngularJS is very suitable for displaying data from a database. Just make sure the data is in JSON format.

Obtain data from a PHP server running MySQL

AngularJS Examples

<div ng-app="myApp" ng-controller="customersCtrl">

  <table>
    <tr ng-repeat="x in names">
    <td>{{ x.Name }}</td>
  <td>{{ x.Country }}</td>
</tr>
</table>
</div>
<script>
var app = angular.module('myApp', []);
  $http.get("customers_mysql.php")
  $http.get("customers_sql.aspx")
.then(function (response) {$scope.names = response.data.records;});
});

</script>

Obtain data from an ASP.NET server running SQL

AngularJS Examples

<div ng-app="myApp" ng-controller="customersCtrl">
<table> <tr ng-repeat="x in names"> <td>{{ x.Name }}</td> <td>{{ x.Country }}</td> </tr> </table> </div> <script> var app = angular.module('myApp', []); app.controller('customersCtrl', function($scope, $http) { $http.get("customers_sql.aspx") .then(function (response) {$scope.names = response.data.records;}); });

</script>

Try It Yourself

Server Code Example

  • The following section lists the server code used to retrieve SQL data.
  • Use PHP and MySQL. Return JSON.
  • Use PHP and MS Access. Return JSON.
  • Use ASP.NET, VB, and MS Access. Return JSON.

Use ASP.NET, Razor, and SQL Lite. Return JSON.

cross-site HTTP requestsRequests for data from different servers (other than the server from which the request page is loaded) are called Cross-site

HTTP requests.

For security reasons, cross-site requests are common on the network. Many pages load CSS, images, and scripts from different servers.those initiated by scriptsCross-site HTTP requests are restricted toSame Site.

In our PHP example, the following lines have been added to allow cross-site access.

header("Access-Control-Allow-Origin: *");

1. Server Code PHP and MySQL

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$conn = new mysqli("myServer", "myUser", "myPassword", "Northwind");
$result = $conn->query("SELECT CompanyName, City, Country FROM Customers");
$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
  if ($outp != "") {$outp .= ",";}
  $outp .= '{"Name":"' . $rs["CompanyName"] . '",'
  $outp .= '"City":"' . $rs["City"] . '",'
  $outp .= '"Country":"'. $rs["Country"] . '"}';
}
$outp ='{"records":['.$outp.']}';
$conn->close();
echo($outp);
?>

2. Server Code PHP and MS Access

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=ISO-8859-1");
$conn = new COM("ADODB.Connection");
$conn->open("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=Northwind.mdb");
$rs = $conn->execute("SELECT CompanyName, City, Country FROM Customers");
$outp = "";
while (!$rs->EOF) {
  if ($outp != "") {$outp .= ",";}
  $outp .= '{"Name":"' . $rs["CompanyName"] . '",'
  $outp .= '"City":"' . $rs["City"] . '",'
  $outp .= '"Country":"'. $rs["Country"] . '"}';
  $rs->MoveNext();
}
$outp ='{"records":['.$outp.']}';
$conn->close();
echo ($outp);
?>

3. Server Code ASP.NET, VB, and MS Access

<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%
Response.AppendHeader("Access-Control-Allow-Origin", "*")
Response.AppendHeader("Content-type", "application/json")
Dim conn As OleDbConnection
Dim objAdapter As OleDbDataAdapter
Dim objTable As DataTable
Dim objRow As DataRow
Dim objDataSet As New DataSet()
Dim outp
Dim c
conn = New OledbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=Northwind.mdb")
objAdapter = New OledbDataAdapter("SELECT CompanyName, City, Country FROM Customers", conn)
objAdapter.Fill(objDataSet, "myTable")
objTable = objDataSet.Tables("myTable")
outp = ""
c = chr(34)
for each x in objTable.Rows
if outp <> "" then outp = outp & ","
outp = outp & "{" & c & "Name" & c & ":" & c & x("CompanyName") & c & ","
outp = outp & c & "City" & c & ":" & c & x("City") & c & ","
outp = outp & c & "Country" & c & ":" & c & x("Country") & c & "}"
next
outp = "{" & c & "records" & c & ":[" & outp & "]}"
response.write(outp)
conn.close
%>

4. Server code ASP.NET, Razor C#, and SQL Lite

@{
Response.AppendHeader("Access-Control-Allow-Origin", "*")
Response.AppendHeader("Content-type", "application/json")
var db = Database.Open("Northwind");
var query = db.Query("SELECT CompanyName, City, Country FROM Customers");
var outp = ""
var c = chr(34)
}
@foreach(var row in query){
if (outp != "") {outp = outp + ","}
outp = outp + "{" + c + "Name"    + c + ":" + c + @row.CompanyName + c + ","
outp = outp +       c + "City"    + c + ":" + c + @row.City        + c + ","
outp = outp +       c + "Country" + c + ":" + c + @row.Country     + c + "}"
}
outp = "{" + c + "records" + c + ":[" + outp + "]}"
@outp