Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

221
Vistas
Call PHP file in Angular2

I am new to Angular2. I have one PHP file and would like to fetch content from PHP file in Angular component. Following is my code I have written by taking references. I am not able to figure out what is the issue as I am not getting any error in console nor desired output.

PHP code - 'getArea.php'

<?php

    $link = mysql_connect('localhost', 'root', '');
    mysql_select_db("mydb", $link);

    $data=array();

    $result = mysql_query("SELECT * FROM dam_color", $link);
    $num_rows = mysql_num_rows($result);

    for ($i=0; $i<mysql_num_rows($result); $i++) 
    {
        $row = mysql_fetch_assoc($result);      
        $data['success'] = true;
        $data['areaname'] = $row['area_name'];
    }

    echo json_encode($data);
?>

Angular Component - php-form.component.ts content

export class PhpFormComponent  {

    msg = "Welcome";
    public data; 

    constructor(private http:Http){ }

    ngOnInit(){
        this.getData();
    }

    getData(){  

        this.http.get('http://localhost/myapi/getArea.php')
        .map(res => res.json())
        .map(res => {

        if(res.success)
        {
            this.msg="Fetched data";
        }
        else
        {
            this.msg="Error in fetching data";
        }
        })
        .subscribe(
            data =>this.getdata = JSON.stringify(data),
            err => console.error(err),
            () => console.log('done')
        );
    }
}
about 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda