4

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')
        );
    }
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
sabin
  • 841
  • 12
  • 15
  • 1
    What is the expected result and what is actually happening instead? – Günter Zöchbauer Apr 10 '17 at 09:39
  • Depending on success/failure, I want to simply display message. Currently nor it's throwing any issue nor proper output. – sabin Apr 10 '17 at 09:41
  • @sabin Tried your code and couldn't reproduce the issue, could you please try and reproduce it in a plunker? – AT82 Apr 10 '17 at 11:26
  • Solved it, it was due to InMemoryWebApiModule and InMemoryDataService. I commented this code and got desired output. Reference http://stackoverflow.com/questions/38432108/angularjs-2-getting-data-from-json-file-not-working – sabin Apr 11 '17 at 14:01

0 Answers0