简体   繁体   中英

Get MySQL data with IONIC

I'm trying to get some data from a MySQL table for the first time.

I'm using ionic , PHP and AngularJS to try it but my query doesn't return anything. As I'm devloping it for my first time, I'm following this tutorial to get data from bd . Although, when I run my code, my file just returns me the content of another file besides of a json file with all the content I want.

Basically I have a function inside my app controller and I'm calling it(for testing) in a button. This functions makes an $http request via Angular to another file with my query. This file that contains the query calls another one to open the database connection e returns the data via Json.

But I just have the first file opened as a return from that process. Can anybody helps me ?

app.js

// Ionic Starter App

// angular.module is a global place for creating, registering and 
retrieving Angular modules

// 'starter' is the name of this angular module example (also set in a 
<body> attribute in index.html)

// the 2nd parameter is an array of 'requires'

var secompApp = angular.module('starter', ['ionic', 'ngCordova'])

.run(function($ionicPlatform) {

$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory 
bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
  cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
  StatusBar.styleDefault();
}

 });

})


secompApp.controller("SeCompController", function($scope, 
$cordovaBarcodeScanner, $http){


$scope.scanBarcode = function(){

$cordovaBarcodeScanner.scan().then(function(imageData){
  //imageData = barcode content
  //TRANSFORMAR ALERT POR FUNÇÃO QUE ENVIA imageData(DADO LIDO) PARA O BD
  alert(imageData.text);
  console.log("format: " + imageData.format);

}, function(error){
  console.log("Ocorreu um erro, tente novamente. Erro:" + error);
});


}

$scope.getMinicursos = function(){
$http.get("ajax/getMinicursos.php").success(function(data){
  $scope.minicursos = data;
  alert(data);
});

  }

});

getMinicursos().php

<?php
require_once '../includes/config.php'; // The mysql database connection script
if(isset($_GET['name'])){
$task = $_GET['name'];
$status = "0";
$created = time();

$query="SELECT * from course";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);

$result = $mysqli->affected_rows;

echo $json_response = json_encode($result);
}

?>

config.php

<?php
$servername = "localhost";
$username = "abc";
$password = "123";

try {
    $conn = new PDO("mysql:host=$servername;dbname=test", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
?> 

My mistake was to use PHP in Ionic localhost, I did not pay atention that wasn't running PHP in a server with it installed to properly "compile" my files and get back the data I was looking for.

So if you're getting back the entire PHP file, not processed by the server, you should make sure that you've PHP installed in your server, even a local/localhost.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM