How to run a PHP with Ionic 2

2

By making the request of my-php.php by GET of Http I literally retorted the code of the php file in a text string , what I need is that the file php be executed and I return the result of the query to the database in Json format

The codes are:

src / providers / servi-products.ts

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

/*
  Generated class for the ServiProductos provider.

  See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  for more info on providers and Angular 2 DI.
*/
@Injectable()
export class ServiProductos {

  constructor(public http: Http) {
    console.log('Hello ServiProductos Provider');
  }


  getPHP(){
    this.http.get("assets/php/mi-php.php").subscribe(data => {
        console.log(data);
    });
  }

}

src / app / app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';

import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { ListPage } from '../pages/list/list';
import { Perfil } from '../pages/perfil/perfil';

import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { ServiProductos } from '../providers/servi-productos';

@NgModule({
  declarations: [
    MyApp,
    HomePage,
    Perfil,
    ListPage
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp, {
      //backButtonText: 'Volver',
      iconMode: 'ios',
      modalEnter: 'modal-slide-in',
      modalLeave: 'modal-slide-out',
      tabsPlacement: 'bottom',
      pageTransition: 'ios-transition'
    }),
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage,
    Perfil,
    ListPage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    ServiProductos,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})
export class AppModule {}

src / page / profile / profile.ts

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { ServiProductos } from '../../providers/servi-productos';

/**
 * Generated class for the Perfil page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-perfil',
  templateUrl: 'perfil.html',
})
export class Perfil {
    login: {username?: string, password?: string} = {};
  submitted = false;

  constructor(public navCtrl: NavController, public navParams: NavParams, public serviProduct: ServiProductos) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad Perfil');
    this.serviProduct.getPHP();
  }

}

src / assets / php / my-php.php

<?php

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
console.log("php corriendo");
system.out.println("some")
$conn = new mysqli("localhost", "root","","nombreBD");


$query="SELECT * FROM nombreTabla;


$result = $conn->query($query);
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
    $rows[] = $r;
}

$conn->close();

echo json_encode($rows);

?> 
    
asked by Daniel Enrique Rodriguez Caste 25.05.2017 в 01:21
source

2 answers

0

I see an error in your code of the question and that is THAT IT IS A DOUBLE KIT IN YOUR SELECT:

$query="SELECT * FROM nombreTabla";


$result = $conn->query($query);
$conn->close();

echo $result;

And second, if you want to obtain data, it would be normal to return a json . (Complementing Jorge's answer)

$result = $conn->query($query);
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
    $rows[] = $r;
}
echo json_encode($rows);
    
answered by 25.05.2017 / 02:51
source
0

You are missing this

$result = $conn->query($query);
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
    $rows[] = $r;
}
header('Content-Type: application/json');
echo json_encode($rows);
    
answered by 25.05.2017 в 02:28