Home Angular 8 Angular 9 with mysql database working example

Angular 9 with mysql database working example

by therichpost
Published: Updated: 12 comments
Angular 9 with mysql database working example

Hello to all, welcome to therichpost.com. Today in this blog post, I am going to tell you, Angular 9 with mysql database working example.

In this post, I am getting the data from pup MySQL database into my angular 9 application and we can use this same code snippet for angular latest versions as well..

Angular 9 with mysql database working example
Angular 9 with mysql database working example
Mysql database working example
Mysql database working example

Guy’s Angular 12 came and if you are new or want to learn more about that then please check below links:

  1. Angular 12 Tutorials.

Here is the complete code snippet and please use carefully:

1. Let start, guy’s 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';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule
  ],
  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';
@Component({
  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 => {
    this.data.push(data);
    console.log(this.data);
   
    
    }, error => console.error(error));
  }
}

 

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

<table>
  <tr>
    <th>Name</th>
    <th>Email</th>
    
  </tr>
  <tr *ngFor="let mydata of data[0]">
    <td>{{mydata.name}}</td>
    <td>{{mydata.email}}</td>
  </tr>

 

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:

<?php
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);
 } 
 else 
 {
     echo "0 results";
 }

 

Angular MySQL Working

Guy’s in the end, don’t forget to run ng serve command into your terminal to check the output.

Guy’s 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

You may also like

12 comments

Mia October 10, 2020 - 12:33 pm

Really great tutorial!

Reply
Ajay Malhotra October 10, 2020 - 3:42 pm

Thank you.

Reply
Rohit Nale January 28, 2021 - 7:00 am

its work…really great tutorial sir…

Reply
Ajay Malhotra January 28, 2021 - 11:00 am

Great and thanks.

Reply
CHUNGYEN TSAI March 10, 2021 - 8:10 pm

hello friend, do you have example for send data to mysql?
thanks you

Reply
Madhura May 22, 2021 - 12:14 pm

hello sir,
I have followed all above steps in my project but shows error in component.ts file@ line this.data.push(data); like
(parameter) data: Object
Argument of type ‘Object’ is not assignable to parameter of type ‘never’.
The ‘Object’ type is assignable to very few other types. Did you mean to use the ‘any’ type instead

I resolved by declaring data:any instead of data=[ ]; after that project executes but @ browser it only shows blank tabs name and email and @ chrome browser console it shows “Failed to load resource: net::ERR_CONNECTION_REFUSED”. Please help me sir how to resolve it.
I also updated my node js and again installed latest angular version before creating project

Reply
Ajay Malhotra May 22, 2021 - 12:25 pm

Check this latest MySQL and angular 12 connectivity.
Thanks

Reply
Madhura May 22, 2021 - 3:45 pm

Yes sir I checked it I have developed same project even I have developed two another seperate project one for add user and another for view user, add user working correctly but view user is not working it just showing blank tabs same error for project MySQL and angular 12 connectivity showing error showing in console of chrome after inspecting like Failed to load resource: net::ERR_CONNECTION_REFUSED
core.js:6210 ERROR HttpErrorResponse pls help me to resolve this database issue with angular

I am using Mysql server with Xampp

Reply
Ajay Malhotra May 22, 2021 - 3:51 pm

Okay I will check with you.

Reply
AVVA TARUN February 22, 2022 - 2:01 pm

The same error I am getting sir…can u please explain how you resolve that

Reply

Leave a Comment

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