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);
?>