PHP - Simple Get MySQLi Data Using AngularJS

Submitted by: 
Language: 
Visitors have accessed this post 101 times.

In this tutorial we will create a Simple Get MySQLi Data Using AngularJS. This code will fetch the MySQLi data when the user click the get button. The code use angular directives to retrieve and encrypt the MySQLi data using json_encode in order to populate the table with angular http.get. This a user-friendly program feel free to modify and use it to your system.

We will be using AngularJS as a framework which has additional custom HTML attributes embedded into it. It can interprets those attributes as directives to bind inputted parts of the page to a model that represent as a standard JavaScript variables.

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_mem, 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_mem");
  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 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="<a href="https://sourcecodester.com"" rel="nofollow">https://sourcecodester.com"</a> >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 - Simple Get MySQLi Data Using AngularJS</h3>
  16. <hr style="border-top:1px dotted #ccc;">
  17. <div class="col-md-2"></div>
  18. <div class="col-md-8">
  19. <button class="btn btn-primary" ng-click="getData();">Get Data</button>
  20. <br /><br />
  21. <table class="table table-bordered">
  22. <thead class="alert-info">
  23. <tr>
  24. <th>Firstname</th>
  25. <th>Lastname</th>
  26. <th>Address</th>
  27. </tr>
  28. </thead>
  29. <tbody>
  30. <tr ng-repeat = "member in members">
  31. <td>{{member.firstname}}</td>
  32. <td>{{member.lastname}}</td>
  33. <td>{{member.address}}</td>
  34. </tr>
  35. </tbody>
  36. </table>
  37. </div>
  38. </div>
  39. <script src="js/angular.js"></script>
  40. <script src="js/script.js"></script>
  41. </body>
  42. </html>

Creating the Main Function

This code contains the main function of the application. This code will fetch the MySQLi data when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as shown below.
data.php

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. $data = array();
  5.  
  6. $query=mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  7.  
  8.  
  9. while($fetch=mysqli_fetch_array($query)){
  10. $data[] = $fetch;
  11. }
  12.  
  13. echo json_encode($data);
  14.  
  15. ?>

script.js
Note: Make sure you save this file inside the js directory in order the script works.

  1. var app = angular.module("myModule", [])
  2. .controller("myController", function($scope, $http){
  3.  
  4. $scope.getData = function(){
  5. $http.get('data.php').then(function(response){
  6. $scope.members = response.data;
  7. });
  8. }
  9. });

There you have it we successfully created a Simple Get MySQLi Data using 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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.