Angular read single data from json

  angular, php

im using php backend to read data out of mysql db on the server. I want to get the data in angular and save it in a variable so i can do simple stuff like displaying it. How can i make something similar to a getter/setter here to read the json?
(browser shows database request is working: Array [ (10) […] ] test.component.ts:18:12 )

Thanks beforehand.

import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {

  data = [];
  constructor(private http: HttpClient) {
    this.http.get('/api/login.php').subscribe(data => {
    this.data.push(data);
    console.log(this.data);
    
    }, error => console.error(error));
  }
}


    <?php
$servername = "...";
$username   = "...";
$password   = "...";
$dbname     = "...";

    // 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 svorname FROM verwaltung_x_schueler WHERE svorname = 'Jan-Niclas'";
    $result = mysqli_query($conn,$sql); 
    $myArray = array();
    if ($result->num_rows > 0) {
    // output data of each row
        while($row = $result->fetch_assoc()) {
            $myArray[] = $row;
        }
        echo json_encode($myArray);
    } 
    else 
    {
        echo "0 results";
    }
    ?>

First I tried following but its not really what I want and it only shows empty rows (9 rows for the 9 results i get, but empty)

<table>
    <tr>
      <th>svorname</th>
      <th>snachname</th>
      
</tr>
<tr *ngFor="let mydata of data[0]">
  <td>{{mydata.svorname}}</td>
  <td>{{mydata.snachname}}</td>
</tr>

Source: Ask PHP

LEAVE A COMMENT