Ionic 2 with php+mysql cann't display data


#1

I am trying to get data from my MySQL database via PHP and display information on Ionic 2 list. I can view my object (data) in the chrome console but i get the following error “Cannot find a differ supporting object ‘[object Object]’ of type ‘object’. NgFor only supports binding to Iterables such as Arrays.”

My Php Code is

    <?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

$conn = new mysqli("localhost", "root", "root", "Northwind");

$result = $conn->query("SELECT CompanyName, City, Country FROM Customers");

$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "") {$outp .= ",";}
    $outp .= '{"Name":"'  . $rs["CompanyName"] . '",';
    $outp .= '"City":"'   . $rs["City"]        . '",';
    $outp .= '"Country":"'. $rs["Country"]     . '"}';
}
$outp = '{"records":['.$outp.']}';
$conn->close();

echo($outp);
?>

My page.html

   <ion-content>
       <ion-list>    
            <ion-item *ngFor="let person of people" >   
                <h2>{{person.Name}}</h2>
            </ion-item>
        </ion-list>
    </ion-content>

ANd page.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

@Component({
  selector: 'page-newcars',
  templateUrl: 'newcars.html'
})
export class NewcarsPage {
  public people: any;
 public tom: any;
  constructor(public navCtrl: NavController, public http: Http) {

     this.loadPeople();
 
 }

 loadPeople(){
    this.load();
  }

  ionViewDidLoad() {
    console.log('Hello NewcarsPage Page');
  }


  load() {
 
  return new Promise(resolve => {
   
    this.http.get('http://54.89.250.225/customers_mysql.php')
      .map(res => res.json())
      .subscribe(data => {
        this.people = data;
        console.log(this.people);
    //    resolve(this.people);
      });
  });
}

}

And my PHP page returns this
Preformatted text{"records":[{"Name":"Tom","City":"Suva","Country":"Fiji"},{"Name":"Jerry","City":"Labasa","Country":"Australia"}]}


#2

You should really just pull the data out into an array and then use echo json_encode(array(“records”=>$result_set));


#3

I’d suggest looking into PDO too,

Your logic could basically turn into this…

<?php $sth = $dbh->prepare("SELECT CompanyName as Name,City,County FROM Customers"); $sth->setFetchMode(PDO::FETCH_OBJ); echo json_encode(array("records"=>$sth->fetchAll());

#4

Hi Thanks , will try it out and see where i am having issues. so my only issue is on the php code as how i pass my values ? As well is there any template or a guide available for this task ?


#5

You mean to add variables to it?

<?php $sth = $dbh->prepare("SELECT CompanyName as Name,City,County FROM Customers WHERE City=:city"); $sth->execute(array("city"=>$city)); $sth->setFetchMode(PDO::FETCH_OBJ); echo json_encode(array("records"=>$sth->fetchAll());

#7

Well, you still need to add the PDO connection string… it would look something like:

try {
$dbh = new PDO(‘mysql:host=localhost;dbname=DatabaseNameHere’, “DatabaseUsername”, “DatabasePassword”);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die();
}