PHP - Display Data Using MySQLi & AngularJS

In this tutorial we will create a Display Data Using MySQLi & AngularJS. AngularJS is a structural framework for dynamic web apps. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/. Lastly, this is the link for the AngularJS https://angularjs.org/.

Creating Database

Open your database web server then create a database name in it db_data, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn = mysqli_connect("localhost", "root", "", "db_data");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en" ng-app="myModule">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body ng-controller="myController">
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Display Data Using MySQLi & AngularJS</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button class="btn btn-primary" ng-click="getData()">Display Data</button>
  18. <br /><br />
  19. <table class="table table-bordered">
  20. <thead class="alert-info">
  21. <tr>
  22. <th>Firstname</th>
  23. <th>Lastname</th>
  24. <th>Address</th>
  25. </tr>
  26. </thead>
  27. <tbody style="background-color:#fff;">
  28. <tr ng-repeat="user in users | orderBy: 'lastname'">
  29. <td>{{user.firstname}}</td>
  30. <td>{{user.lastname}}</td>
  31. <td>{{user.address}}</td>
  32. </tr>
  33. </tbody>
  34. </table>
  35. </div>
  36. <script src="js/angular.js"></script>
  37. <script src="js/script.js"></script>
  38. </body>
  39. </html>

Creating the PHP Query

This code contains the php query of the application. This code will sent fetch request to the server to display back the data to the table. To do this just copy and write these block of codes inside the text editor and save it as data.php.
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. $data = array();
  5.  
  6. $query = mysqli_query($conn, "SELECT * FROM `user`") or die(mysqli_error());
  7.  
  8. while($fetch = mysqli_fetch_array($query)){
  9. $data[] = $fetch;
  10. }
  11.  
  12. echo json_encode($data);
  13.  
  14. ?>

Creating the Main Function

This code contains the main function of the application. This code will get the php fetch request and display the data thru angular directives. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. var app = angular.module("myModule", [])
  2. .controller("myController", function($scope, $http){
  3. $scope.getData = function(){
  4. $http.get('data.php').then(function(response){
  5. $scope.users = response.data;
  6. });
  7. }
  8. });
There you have it we successfully created a Display Data Using MySQLi & AngularJS. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Add new comment