AngularJS SQL

AngularJS SQL: A database is very useful when it comes to the creation of website applications. It enables us to store our data in it, and then be in a position to fetch the data whenever there is a need to do so. This data can then be displayed to the users as a way of informing them. A web application is never complete unless it has a database with it.

AngularJS SQL Example

Consider the code given below which shows how AngularJS can be used for fetching data from a PHP server which has SQL running in it.

<!DOCTYPE html>
<html>
<style>
table, th, td {
border: 1px solid grey;
border-collapse: collapse;
padding: 6px;
}
table tr:nth-child(odd) {
background-color: #f2f2f1;
}
table tr:nth-child(even) {
background-color: #f2f2ff;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>
<div ng-app="myApplication" ng-controller="cusCtrl">
<table>
<tr ng-repeat="p in names">
<td>{{p.Name }}</td>
<td>{{p.Country}}</td>
</tr>
</table> tr:nth-child(even) {
</div>
<script>
var application = angular.module('myApplication', 1[]);
application.controller('ctrl', function($scope,$http) {
$http.get("http://www.tutorials.freshersnow.com")
success(function (response) 
($scope.names = response.records;});
});
</script>
</body>
</html>

That is how the data can be fetched. It is running on a MySQL database on a PHP server. The database contains records about our customers, and we need to fetch the records which provide us with the data for these customers. The result should then be displayed in a table which the users will view.

To get records from ASP.NET server Example

Suppose we have an ASP.NET server that is running SQL in it. The SQL has the database with records, and our aim is to use AngularJS so as to fetch the records. This can be done as shown below.

<!DOCTYPE html>
<html>
<style>
table, th, td {
border: 2px solid grey;
border-collapse: collapse;
padding: 6px;
}
table tr:nth-child(odd). {
background- color: #f2f2f2;
}
table tr:nth-child (even) {
background-color: #ffffff;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>
<div ng-app-"myApplication" ng-controller="cusCtrl">
<table>
<tr ng-repeat="p in names">
<td>{{p.Name }}</td>
<td>{{p.Country }}</td>
</tr>
< /table>
</div>
<script>
var application = angular.module ('myApplication', []);
application.controller('cusCtrl', function($scope,$http) {
$http.get("http://www.mysite.com/angular/cus_sql.aspx")
success(function (response) {$scope.names =response.records;});
});
</script>
</body>
</html>

That is how fetching can be done. The database needs to be specified correctly, otherwise, you will get a wrong output or even an error. If you do it well, the data regarding your customers will be displayed very well. That is how the fetching of an SQL database can be done with AngularJS. Note that in this case, we have used an ASP.NET server.

Cross-Site HTTP Requests

These are a type of requests in which the user requests for data from a different server. As far as web applications are concerned, cross-site HTTP requests are very common. The requested data from a different server might be scripts, CSS, or images. However, there should be restrictions when this is to happen for the purpose of ensuring that there is security. For instance, when scripts are to be involved, this is only allowed for a single or the same site, otherwise, the request will not be accepted.

PHP Code for Cross-Site

Consider the PHP line of code given below which shows how cross-site access can be permitted. Here is the line.

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

PHP and MySQL Server Code Example

The following is a server code for both PHP and MySQL.

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

That is how the code should be.

To get access to Database with Server code  Example

For accessing an access database with a server code PHP, this should be as follows.

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

The code will access the database which we have in Access and then display the details. The details are describing the customers that we have.

For VB and MS Access Example

In the case of VB and MS Access, when we have the server Asp.NET, we can access it as follows:

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

That is the code that should be used.

For VB Razor and SQL Lite Example

For VB Razor and SQL Lite, with ASP.NET, the code should be as shown below:

@{
Response.AppendHeader("Access-Control","*")
Response.AppendHeader("Content-type", "application/json")
var database = Database.Open("MyDb");
var query = database.Query("SELECT Company,
City, Country FROM Customers");
var out =""
var c chr(34)
}
@foreach(var row in query)
{
if out <> "" then out out +","
out = out + "["+ c + "Name" + c + " " + c +@row.
Company + c +","
out = out + c + "City" + c + ":" + c + @row.City
out = out + c + "Country" + c + ":" + C + @row.
Country + c + ""}"
}
out="{"+ c + "records"+ c + ":[" + out + "]}"
@out