简体   繁体   English

使用IONIC获取MySQL数据

[英]Get MySQL data with IONIC

I'm trying to get some data from a MySQL table for the first time. 我正在尝试第一次从MySQL表中获取一些数据。

I'm using ionic , PHP and AngularJS to try it but my query doesn't return anything. 我正在使用ionic,PHP和AngularJS进行尝试,但我的查询未返回任何内容。 As I'm devloping it for my first time, I'm following this tutorial to get data from bd . 当我第一次开发它时,我正在按照本教程从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. 虽然,当我运行代码时,我的文件只是返回给我除json文件之外的另一个文件的内容,该文件包含我想要的所有内容。

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. 这个函数通过我的查询通过Angular向另一个文件发出$ http请求。 This file that contains the query calls another one to open the database connection e returns the data via Json. 该包含查询的文件调用另一个文件来打开数据库连接,e通过Json返回数据。

But I just have the first file opened as a return from that process. 但是我只是打开了第一个文件,作为该过程的回报。 Can anybody helps me ? 有人可以帮我吗?

app.js 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 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 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. 我的错误是在Ionic本地主机中使用PHP,我没有注意未在安装了该服务器的服务器上运行PHP的情况,以正确“编译”我的文件并取回我要查找的数据。

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. 因此,如果要取回服务器未处理的整个PHP文件,则应确保服务器中已安装PHP,甚至本地/本地主机也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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