使用IONIC获取MySQL数据

Get MySQL data with IONIC

本文关键字:数据 MySQL 获取 IONIC 使用      更新时间:2023-09-26

我第一次尝试从MySQL表中获取一些数据。

我使用ionic, PHP和AngularJS来尝试它,但我的查询没有返回任何东西。当我第一次开发它时,我遵循本教程从bd获取数据。虽然,当我运行我的代码时,我的文件只是返回给我另一个文件的内容,除了包含我想要的所有内容的json文件。

基本上我有一个函数在我的应用程序控制器和我调用它(测试)在一个按钮。这个函数通过Angular向另一个文件发出$http请求。这个包含查询的文件调用另一个文件打开数据库连接,并通过Json返回数据。

但是我只是打开了第一个文件作为该过程的返回。有人能帮我吗?

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
$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();
    }
?> 

我的错误是在Ionic localhost中使用PHP,我没有注意到没有在服务器中运行PHP,并安装了它来正确"编译"我的文件并获取我正在寻找的数据。

如果您要返回整个PHP文件,而不是由服务器处理,则应确保服务器中安装了PHP,甚至是本地/localhost。