How to Fetch MySQL Data Using AngularJS in PHP

July 14, 2020
PHP
0 0
How to Fetch MySQL Data Using AngularJS in PHP

Tutorial: How to Fetch MySQL Data Using AngularJS in PHP with Source Code

In this tutorial, we will create on How to Fetch MySQL Data Using AngularJS in PHP. This code will retrieve the MySQLi server when the user clicks the get button. The system uses angular directives to retrieve and process the MySQLi server to encrypt the selected data using json_encode, then populate the HTML 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 interpret those attributes as directives to bind inputted parts of the page to a model that represents a standard JavaScript variable.

Getting Started:

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the 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_fetching; after that, click Import, then locate the database file inside the folder of the application then click ok.

How to Fetch MySQL Data Using AngularJS in PHP

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.

<?php
    $conn=mysqli_connect("localhost", "root", "", "db_fetching");
 
    if(!$conn){
        die("Error: Failed to connect to database!");
    }
?>

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.

<!DOCTYPE html>
<html lang="en" ng-app="myModule">
    <head>
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
    </head>
<body ng-controller="myController">
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="https://campcodes.com" >CampCodes</a>
        </div>
    </nav>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Fetching MySQLi Server Using AngularJS</h3>
        <hr style="border-top:1px dotted #ccc;">
        <div class="col-md-8">
            <table class="table table-bordered">
                <thead class="alert-info">
                    <tr>
                        <th>Firstname</th>
                        <th>Lastname</th>
                        <th>Gender</th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat = "student in students">
                        <td>{{student.firstname}}</td>
                        <td>{{student.lastname}}</td>
                        <td>{{student.gender}}</td>
                    </tr>
                </tbody>
            </table>
        </div>
        <div class="col-md-4">
            <center><button class="btn btn-primary" ng-click="fetchAll()">Fetch All</button></center>
            <br />
            <center><button class="btn btn-info" ng-click="fetchOne()">Fetch One</button></center>
        </div>
    </div>

http://js/angular.js


http://js/script.js

</body>	
</html>

Creating the Main Function

This code contains the main function of the application. This code will retrieve the MySQLi server 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.
data1.php

<?php
    require_once 'conn.php';
 
    $data = array();
 
    $query=mysqli_query($conn, "SELECT * FROM `student`") or die(mysqli_error());
 
 
    while($fetch=mysqli_fetch_array($query)){
        $data[] = $fetch;
    }
 
    echo json_encode($data);
 
?>

data2.php

<?php
    require_once 'conn.php';
 
    $data = array();
 
    $query=mysqli_query($conn, "SELECT * FROM `student` ORDER BY rand()") or die(mysqli_error());
 
 
    $fetch=mysqli_fetch_array($query);
    $data[] = $fetch;
 
 
    echo json_encode($data);
 
?>

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

var app = angular.module("myModule", [])
                .controller("myController", function($scope, $http){
 
                    $scope.fetchAll = function(){
                        $http.get('data1.php').then(function(response){
                            $scope.students = response.data;
                        });
                    }
 
                    $scope.fetchOne = function(){
                        $http.get('data2.php').then(function(response){
                            $scope.students = response.data;
                        });
                    }
 
 
                });

There you have it we successfully created a Fetching MySQLi Server 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!!

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *