Professional Documents
Culture Documents
HOME > PHP > HOW TO WORK WITH ANGULARJS AS FRONTEND AND PHP
MYSQL AS BACKEND
In this tutorial you will learn that how to work with AngularJs as web frontend or in any
hybrid app and get retrieve data from database using php and mysql. We have created restful
webservice for return records to angular app.
Now steps for making a angularjs app with php and mysql –
Step 1) Create a database and table for fetching content from the table
https://www.w3school.info/2016/02/20/howtoworkwithangularjsasfrontendandphpmysqlasbackend/ 1/7
2/27/2019 How to work with AngularJS as frontend and PHP MYSQL as backend W3SCHOOL
Note: If you want to use existing database then select you db else create a new db with
‘news_db’ or whatever you want to take a db name.
Now Create a table named ‘news’ for getting data by following sql statement
1 --
2 -- Dumping data for table `news`
3 --
4
5 INSERT INTO `news` (`id`, `title`, `permalink`, `details`, `thumbnail`, `category_id
6 (1, 'MY NEWS TITLE 1', '', 'The standard chunk of Lorem Ipsum used since the 1500s is reproduce
7 (2, 'MY NEWS TITLE 2', '', 'Lorem Ipsum is simply dummy text of the printing and typesetting in
1 <?php
2 $details=array();
3 $details['server_host']="localhost";//server host name
4 $details['mysql_name']="root";//your mysql user name
5 $details['mysql_password']="";//your mysql user name
6 $details['mysql_database']="news_db";//your database name
7 ?>
1 <?php
2 header("Access-Control-Allow-Origin: *");
3 header("Content-Type: application/json; charset=UTF-8");
4 ?>
Now code for getting data from database and provide in restful json format by following
code
1 <?php
2 include 'config.php';//make the cofig file include
3 global $details;//make the connection vars global
4
https://www.w3school.info/2016/02/20/howtoworkwithangularjsasfrontendandphpmysqlasbackend/ 2/7
2/27/2019 How to work with AngularJS as frontend and PHP MYSQL as backend W3SCHOOL
5 if($_GET['method'] == "load_news")
6 {
7 $conn = new mysqli($details['server_host'], $details['mysql_name'],$details['mysql_passwor
8 $result = $conn->query("SELECT title,details,hits FROM news");
9 $data=array();
10 while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
11 $row=array();
12 $row['title']=addslashes($rs["title"]);
13 $row['details']=addslashes($rs["details"]);
14 $row['hits']=addslashes($rs["hits"]);
15
16 $data[]=$row;
17
18 }
19 $jsonData=array();
20 $jsonData['records']=$data;
21
22 $conn->close();
23 echo json_encode($jsonData);
24
25 }
26 ?>
1 <?php
2 header("Access-Control-Allow-Origin: *");
3 header("Content-Type: application/json; charset=UTF-8");
4 include 'config.php';//make the cofig file include
5 global $details;//make the connection vars global
6
7 if($_GET['method'] == "load_news")
8 {
9 $conn = new mysqli($details['server_host'], $details['mysql_name'],$details['mysql_passwor
10 $result = $conn->query("SELECT title,details,hits FROM news");
11 $data=array();
12 while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
13 $row=array();
14 $row['title']=addslashes($rs["title"]);
15 $row['details']=addslashes($rs["details"]);
16 $row['hits']=addslashes($rs["hits"]);
17
18 $data[]=$row;
19
20 }
21 $jsonData=array();
22 $jsonData['records']=$data;
23
24 $conn->close();
25 echo json_encode($jsonData);
26
27 }
28 ?>
1 <script src="js/angular.1.4.8.min.js"></script>
https://www.w3school.info/2016/02/20/howtoworkwithangularjsasfrontendandphpmysqlasbackend/ 3/7
2/27/2019 How to work with AngularJS as frontend and PHP MYSQL as backend W3SCHOOL
make a html table format inside the div for getting data from datasource(php/mysql restful
service) as following
Now put the script that call the restful web service and load data to the table
1 <script>
2 var app = angular.module('myApp', []);
3 app.controller('newsCtrl', function($scope, $http) {
4 $http.get("http://localhost/angular/wsdl.php?method=load_news")
5 .then(function (response) {$scope.names = response.data.records;});
6 });
7 </script>
1 <script src="js/angular.1.4.8.min.js"></script>
2 <div ng-app="myApp" ng-controller="newsCtrl">
3
4 <table border="2">
5 <tr ng-repeat="x in names">
6 <td>{{ x.title }}</td>
7 <td>{{ x.details }}</td>
8 <td>{{ x.hits }}</td>
9 </tr>
10 </table>
11
12 </div>
13
14 <script>
15 var app = angular.module('myApp', []);
16 app.controller('newsCtrl', function($scope, $http) {
17 $http.get("http://localhost/angular/wsdl.php?method=load_news")
18 .then(function (response) {$scope.names = response.data.records;});
19 });
20 </script>
Congratulations you have completed with this tutorial please provide comments for this
tutorial. DOWNLOAD COMPLETE CODE
Previous Post
Array to Xml Conversion and Xml to Array Convert in PHP
https://www.w3school.info/2016/02/20/howtoworkwithangularjsasfrontendandphpmysqlasbackend/ 4/7
2/27/2019 How to work with AngularJS as frontend and PHP MYSQL as backend W3SCHOOL
Next Post
Custom form validation in Codeigniter
Jeetendra Singh
Jeetendra is a Web Developer, technical writer and a blogger from
jaipur. He's worked with PHP,Mysql,JavaScript,Node Js,Angular
Js,Jquery,JQuery Mobile,.Net,Sql Server etc., but his language of
choice is PHP & JAVASCRIPT. You can learn more and discussion
with him at w3school
https://www.w3school.info/2016/02/20/howtoworkwithangularjsasfrontendandphpmysqlasbackend/ 5/7
2/27/2019 How to work with AngularJS as frontend and PHP MYSQL as backend W3SCHOOL
1 Comment w3school
1 Login
Join the discussion…
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
Esinniobiwa Quareeb • a year ago
nice one
33 △ ▽ • Reply • Share ›
ALSO ON W3SCHOOL
Codeigniter best tricks and hacks Progress bar for php form post ,
2 comments • 2 years ago Download the Code
w3school — Yes chintan it is really very 1 comment • a year ago
straight solutions which I came to know in Jack — i can't download. the facebook
some of my projects, button dies not work
Array to Xml Conversion and Xml to Stripe Subscriptions using php –
Array Convert in PHP Download Full Source Code
1 comment • 3 years ago 9 comments • 2 years ago
daud khan — Hi,Do you like to try tom macdougall — Hi I am currently in
Laravel. Please come and join us the process of building my first
athttps://www.facebook.com/la... application and am wondering if this will
work for paying …
✉ Subscribe d Add Disqus to your siteAdd DisqusAdd
🔒 Disqus' Privacy PolicyPrivacy PolicyPrivacy
Search... Go
SPONCERED BY
https://www.w3school.info/2016/02/20/howtoworkwithangularjsasfrontendandphpmysqlasbackend/ 6/7
2/27/2019 How to work with AngularJS as frontend and PHP MYSQL as backend W3SCHOOL
Name:
E-mail:
Subscribe
RECENT POSTS
CATEGORIES
Angular js
Apis
Codeigniter
javascript
jQuery
Mean Stack
https://www.w3school.info/2016/02/20/howtoworkwithangularjsasfrontendandphpmysqlasbackend/ 7/7