AngularJS SQL


AngularJS is perfect for displaying data from a Database. Just make sure the data is in JSON format.


Fetching Data From a PHP Server Running MySQL

Output :



Fetching Data From an ASP.NET Server Running SQL

Output :




Server Code Examples

The following section is a listing of the server code used to fetch SQL data.

  • Using PHP and MySQL. Returning JSON.
  • Using PHP and MS Access. Returning JSON.
  • Using ASP.NET, VB, and MS Access. Returning JSON.
  • Using ASP.NET, Razor, and SQLite. Returning JSON.

Cross-Site HTTP Requests

A request for data from a different server (other than the requesting page) is called a cross-site HTTP request.

Cross-site requests are common on the web. Many pages load CSS, images, and scripts from different servers.

In modern browsers, cross-site HTTP requests from scripts are restricted to the same site for security reasons.

The following line, in our PHP examples, has been added to allow cross-site access:

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

This is a PHP header used for Cross-Origin Resource Sharing (CORS). It allows resources on this server to be accessed by any domain (the `*` wildcard).

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

<%@ 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