Home Angular 8 Angular 8 with mysql database working example

Angular 8 with mysql database working example

by therichpost
62 comments

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

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

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

phpmysql data in angular 8
php mysql data in angular 8

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 Angular8 atmosphere

ng new angular8phpmyadmindatabse //Install New Angular App

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

cd angular8phpmyadmindatabse //Go inside the Angular 8 Project

 

2. After come inside angular8phpmyadmindatabse folder, please add below code into angular8phpmyadmindatabse\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 angular8phpmyadmindatabse\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 = 'angular8phpmyadmindatabse';
  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 angular8phpmyadmindatabse\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. For good looks, add below code into app.component.css file:

table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
  }
  
  td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
  }
  
  tr:nth-child(even) {
    background-color: #dddddd;
  }

 

6. 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
$servername = "localhost";
$username   = "root";
$password   = "";
$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";
}
?>

 

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

You may also like

62 comments

parkhya January 17, 2020 - 9:35 am

very help full code….

Reply
Ajay Malhotra February 1, 2020 - 4:27 pm

Thank you

Reply
aqib February 5, 2020 - 6:59 am

it’s not working for me.

Reply
Ajay Malhotra February 5, 2020 - 3:59 pm

Tell me your issue, please.

Reply
aqib February 7, 2020 - 7:25 am

i have multiple modules and components not getting idea how to implement.

Reply
Ajay Malhotra February 7, 2020 - 7:29 am

Email me and share your code and for more help. Or click on Contact ME Menu to contact.

Reply
MR RICHARD PIERCE February 12, 2020 - 2:30 am

I have angular 9 and vs code and chrome and I believe I have set up my example as you have done
I have added the following line to the top of my sql.php file
header(‘Access-Control-Allow-Origin: *’, false);

I’m stuck with a CORS problem, the chrome console says

Angular is running in the development mode. Call enableProdMode() to enable the production mode.
localhost/:1 Access to XMLHttpRequest at ‘http://localhost/sql.php’ from origin ‘http://localhost:4200’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
app.component.ts:27 HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: “Unknown Error”, url: “http://localhost/sql.php”, ok: false, …}
(anonymous) @ app.component.ts:27

Is angular 9 different?

Reply
Ajay Malhotra February 12, 2020 - 4:10 pm

No, angular 9 is not different, did you try my complete code?

Reply
MR RICHARD PIERCE February 14, 2020 - 6:17 am

This problem turned out to be
a) I had an incorrect address for the php file in the http.get, it wasn’t a CORS issue at all even though that was the first error message but a 404 error, the second error message (not shown).
b) I moved the whole project to wwwroot folder. I think my copy of IIS isn’t set right for any other folder.
Any way, it runs properly now. Thank you.

Reply
Ajay malhotra February 14, 2020 - 6:19 am

Did you have xampp?

Reply
MR RICHARD PIERCE February 14, 2020 - 9:36 am

I use MySQL WorkBench 6.3

seiha February 19, 2020 - 3:23 pm

<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST");
header("Access-Control-Allow-Headers: *");
header("Content-Type: application/json; charset=utf-8");
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "employee";

fa July 16, 2021 - 4:55 am

Ok

Reply
Ajay malhotra February 14, 2020 - 9:43 am

don’t worry, Today I will share post Angular9 with mysql

Reply
Ajay malhotra February 14, 2020 - 9:43 am

don’t worry, Today I will share post Angular9 with mysql

Reply
seiha February 19, 2020 - 3:20 pm

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”;
}
?>

Reply
seiha February 19, 2020 - 3:21 pm

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”;
}
?>

=====please follow like this======

Reply
Ajay Malhotra February 19, 2020 - 5:05 pm

Great..

Reply
Jaclyn Gates March 3, 2020 - 6:40 pm

For the employee.php file, where would be the equivalent location to place it in when working without XAMPP? I have a MySQL community server running with MySQL Workbench as the GUI. What would be the right place to put the employee.php when working with my local MySQL community server? There are no htdocs folders anywhere for Workbench?

Reply
bmr June 18, 2020 - 9:17 am

crud tutorial please with angular 7 and php mysql

Reply
bmr June 18, 2020 - 11:31 am

please upload edit/update data angular

Reply
Ajay Malhotra June 18, 2020 - 4:34 pm

Sure

Reply
Jaclyn Gates March 2, 2020 - 6:27 pm

For the employee.php file, I’m using MySQL workbench as an alternative to XAMPP. I have a local MySQL server running on my PC with MySQL workbench as the GUI. How would I go about adding the employee.php file in step 6 to connect to my Angular Live development server? The Angular Live development server is running just fine on its own, its just a blank page with the “Name” and “Email” tabs at the top of the page

Reply
Altaaf Hamod March 9, 2020 - 5:22 am

Extremely helpful code, Thank you! But is there anyway to write back data from angular to php to the db?

Reply
Ajay Malhotra March 9, 2020 - 3:00 pm

Yes and you can find that post on my blog too 🙂

Reply
Jaclyn Gates March 10, 2020 - 3:48 pm

Please reply back to my previous question

Reply
Ajay Malhotra March 10, 2020 - 4:27 pm

I will update you with the post link.

Reply
sss March 23, 2020 - 2:47 pm

comment je peut afficher foreign key dans un tableau en angular????

Reply
Ajay Malhotra March 24, 2020 - 7:23 am

Did you get you?

Reply
Rishi April 5, 2020 - 3:21 pm

Is the code works for Wamp server??(wamp server has in built MySQL phpmyadmin)

Reply
Ajay Malhotra April 6, 2020 - 10:41 am

yes

Reply
farahin April 16, 2020 - 7:37 am

hi ajay, can u help me with my code. the data from my database is not display in the table. i have a different php file a bit and i don’t know how to implement ur php code into mine.

Reply
Ajay Malhotra April 16, 2020 - 7:44 am

Hi Farahin, you can email me and you can ask send you query:
therichposts@gmail.com

Reply
Kapil April 21, 2020 - 2:50 pm

Tables not showing data values on web, Console.log showing data values:

Reply
Ajay Malhotra April 21, 2020 - 4:42 pm

Try like that:
*ngFor=”let mydata of data

Reply
Richard May 12, 2020 - 2:36 pm

hello,

Can you add the post method which allows to add a user

thanks.

Reply
lahiru May 14, 2020 - 7:21 am

Please send me the source code.

Reply
Ajay Malhotra May 14, 2020 - 7:24 am

code is here bro

Reply
ankit May 25, 2020 - 9:54 pm

HttpErrorResponse {headers: HttpHeaders, status: 201, statusText: “Created”, url: “http://localhost:8080/AngularLoginRegister//insert.php”, ok: false, …}
error: {error: SyntaxError: Unexpected token s in JSON at position 0 at JSON.parse () at XMLHtt…, text: “stdClass Object↵(↵ [id] => ↵ [username] => d…me”:”dfg”,”password”:”dfg”,”email”:”dfg”,”id”:59}”}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: “Http failure during parsing for http://localhost:8080/AngularLoginRegister//insert.php
name: “HttpErrorResponse”
ok: false
status: 201
statusText: “Created”
url: “http://localhost:8080/AngularLoginRegister//insert.php”
__proto__: HttpResponseBase

Reply
Ajay Malhotra May 26, 2020 - 4:57 am

Hi, i think below issue is on your php json created data:
in JSON at position 0 at JSON.parse () at…

Reply
Pp March 8, 2021 - 11:00 pm

Hi Ajay..thanks for the tutorial..I am getting the same error as Ankit above. How to solve this error ? Thanks

Reply
Ajay Malhotra March 9, 2021 - 3:55 am

Okay I will update you both.

Reply
Reeb July 7, 2020 - 9:37 pm

Hello. I just wanted to drop a huge THANK YOU for such a nice and clear example.

Also, in my case the link used in app.component.ts is http://localhost:8888/employee.php
(just sayin’… for others that might have this issue also).

Cheers.

Reply
Ajay Malhotra July 8, 2020 - 5:21 am

Thank you.
employee.php file is in my xampp/htdocs folder and I am getting data from there.

Reply
vipina July 15, 2020 - 10:42 am

how uplode Angular8 with mysql and php on GoDaddy
can help me I did prod command and made its folder but hot do on diploy

Reply
Ajay Malhotra July 15, 2020 - 11:50 am

Very first, you need to make build then you can easily upload on godaddy then it will work easily.

Reply
vipina July 16, 2020 - 10:43 am

hi
my I want to upload a website with PHP and MySQL frontend with angular 8 but I know with angular just prod built that create dist folder but how to put the path for MySQL inside the angular8 before run command for build and where I need to put PHP all file on godaddy c-panel

Reply
Ajay Malhotra July 16, 2020 - 10:50 am

Why this, “how to put the path for MySQL inside the angular 8”?
Just need to add API in angular and that will connect with php mysql backend.

Reply
Helen August 4, 2020 - 4:44 am

Could you give us examples on insert, delete and update to the database as well?

Reply
Koms August 7, 2020 - 10:31 am

My Php project uses Flash as 3rd part and I want to replace it with angular.I have installed angular running at port 4200 by default and php in htdocs folder at folder 8080,how can I send request and how will the build be generated?

Reply
Ajay Malhotra August 7, 2020 - 1:17 pm

Simple, with uses angular default HTTPCLIENT service.
Thanks

Reply
Koms August 7, 2020 - 6:18 pm

error: error { target: XMLHttpRequest, isTrusted: true, lengthComputable: false, … }

headers: Object { normalizedNames: Map(0), lazyUpdate: null, headers: Map(0) }

message: “Http failure response for localhost:8080/userLogin.php: 0 Unknown
Error”

name: “HttpErrorResponse”

ok: false

status: 0

statusText: “Unknown Error”

Reply
Therichpost August 7, 2020 - 6:22 pm

Hi, I need to check your code.

Reply
andrest September 11, 2020 - 3:43 pm

wow… this is good tutorial… thanks for sharing..

Reply
Ajay Malhotra September 11, 2020 - 3:54 pm

Thank you and welcome.

Reply
Asif September 21, 2020 - 7:05 pm

Can you tell us how to add security in api calls? As these calls are available for user to see in network tab in developer mode.

Reply
Ajay Malhotra September 22, 2020 - 4:33 am

I will update you, thanks.

Reply
Shrikanth November 8, 2020 - 5:32 am

Excellent work Ajay. Thank you . It worked perfectly . Will you please let me know how to select desired record from the table. Example : Employeee.php file can be updated as select * from employee where name =’Some Name” . Can you please explain how to pass Some Name value from Html file to TS file To Php file please …

Reply
Ajay Malhotra November 8, 2020 - 5:35 am Reply

Leave a Comment

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