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')
        );
    }
}
4
  • 1
    What is the expected result and what is actually happening instead? Commented Apr 10, 2017 at 9:39
  • Depending on success/failure, I want to simply display message. Currently nor it's throwing any issue nor proper output. Commented Apr 10, 2017 at 9:41
  • @sabin Tried your code and couldn't reproduce the issue, could you please try and reproduce it in a plunker? Commented Apr 10, 2017 at 11:26
  • Solved it, it was due to InMemoryWebApiModule and InMemoryDataService. I commented this code and got desired output. Reference stackoverflow.com/questions/38432108/… Commented Apr 11, 2017 at 14:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.