Angular 8Angular 9Mysql

Angular 9 with mysql database working example

Angular 9 with mysql database working example

Hello to all, welcome to In this post, I will tell you, Angular 9 with mysql database working example.

I have shared many posts related to Angular9 and you all loved it very much so today I came up with new post related to angular 9 and php mysql database combination.

In this post, I am getting the data from php mysql database to angular 9.

Mysql database working example

Here are complete code snippets and please all this into your files very carefully:

1. Let start, here are the basics commands to set Angular 8 into your pc:

npm install -g @angular/cli //Setup Angular9 atmosphere

ng new angular9phpmyadmindatabse //Install New Angular App

/**You need to update your Nodejs also for this verison**/

cd angular9phpmyadmindatabse //Go inside the Angular 8 Project


2. After come inside angular9phpmyadmindatabse folder, please add below code into angular9phpmyadmindatabse\src\app\app.module.ts file:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';

  declarations: [
  imports: [
  providers: [],
  bootstrap: [AppComponent]
export class AppModule { }


3. Now add below code into angular9phpmyadmindatabse\src\app\app.component.ts file:

import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
export class AppComponent {
  title = 'angular9phpmyadmindatabse';
  data = [];
  constructor(private http: HttpClient) {
    this.http.get('http://localhost/employee.php').subscribe(data => {;
    }, error => console.error(error));


4. Now add below code into angular9phpmyadmindatabse\src\app\app.component.html file:

  <tr *ngFor="let mydata of data[0]">


5. Finally for mysql data, please add below code into employee.php file into your htdocs folder and don’t forget to start your XAMPP:

header('Access-Control-Allow-Origin: *');
 $servername = "localhost";
 $username   = "root";
 $password   = "root";
 $dbname     = "employee";
 // Create connection
 $conn = new mysqli($servername, $username, $password, $dbname);
 // Check connection
 if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
   //echo "Connected successfully";
 $sql = "SELECT * FROM emplyee";
 $result = mysqli_query($conn,$sql); 
 $myArray = array();
 if ($result->num_rows > 0) {
 // output data of each row
     while($row = $result->fetch_assoc()) {
         $myArray[] = $row;
     print json_encode($myArray);
     echo "0 results";


In the end, don’t forget to run ng serve command into your terminal.

If you have any kind of query related to this post or related to Angular then please contact me or comment below.

Jassa Jatt

Angular Expert

Thank you

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, Angular 8, Angular 9, Angular 10, Angular 11, Nodejs, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.