将值发布到php文件中以执行sql查询并以json格式获取数据 [英] Post value to a php file to execute sql query and get back data in json format

查看:40
本文介绍了将值发布到php文件中以执行sql查询并以json格式获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Flutter,并试图构建一个可以显示我今天的课程的移动应用程序.而且我想了解有关数据处理的更多信息,所以我尝试使用php并从数据库中获取数据.

I am learning Flutter and trying to build a mobile application that can display my today courses. And I want to learn more about data handling so I try to use php and get data from database.

我的目标是我想将日期值发布到php文件中,然后执行sql查询并以json格式获取数据.最后,数据将显示在Flutter应用中.

My target is that I want to post the date value to the php file, then execute the sql query and get the data in json format. Finally, the data will be displayed in a Flutter app.

我的php文件的一部分如下:

A part of my php file is as follow:

$date = $_POST["date"];

$sql = "SELECT * FROM Table WHERE Date = '$date'";

$response=array();
$final=array();
$response["status"] = "fail";
$result = mysqli_query($con, $sql);

while($row=mysqli_fetch_assoc($result)){
   $response["status"] = "success";
   $final[] = $row;
}

$response["result"]=$final;

echo json_encode($response);

从服务器获取数据的apiProvider.dart文件.

The apiProvider.dart file to get data from the server.

import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' show Client;
import 'package:http/http.dart' as http;

class ApiProvider {

  Client client = Client();
  final String apiURL = "my.php";
  final String today = '2019-06-18';

  // Use to fetch today courses.
  Future<CourseListModel> getCourses() async {

    http.post(apiURL, body: {"date": today});

    final response = await client.get(apiURL);

    if (response.statusCode == 200) {
      // If the API call to the server was successful, parse the course list in JSON format
      return CourseListModel.fromJson(json.decode(response.body));
    }
    else {
      // If the API call was fail, throw an error.
      throw Exception('Response content length is ${response.statusCode}, failed to get today courses.');
    }
  }
}

运行Flutter应用程序时无法显示课程数据.我认为"today"值无法正确解析到php文件.我已经完成了Google搜索并尝试了其他方法,但仍然无法正常工作.我不太了解如何处理POST方法.因此,有人可以给我一些解决问题的提示或建议吗?谢谢.

The course data cannot be shown when I run the Flutter app. I think the "today" value cannot be parse to the php file correctly. I have done Google search and tried different methods but it still not work. I am not quite understand how to handle the POST method. So can anyone give me some hints or suggestions to solve the problem? Thanks.

===========更新========

=========== Updated ==========

我已经使用 Ephenodrom 的方法解决了我的问题.更新后的dart文件如下:

I have solved my problem by using the Ephenodrom's method. Updated dart file is as follow:

class ApiProvider {
...
  Future<CourseListModel> getCourses() async {

    final response = await http.post(apiURL, body:{"date": today});
...
}

谢谢大家:D

推荐答案

您的问题

您发出POST请求,然后发出GET请求.您应该在POST请求中从您的php脚本获得响应.此后不必发出GET请求.

更改此内容:


http.post(apiURL, body: {"date": today});
final response = await client.get(apiURL);

对此:


final response = http.post(apiURL, body: {"date": today});

我强烈建议您查看以下信息!

I would highly recommend to check out the information below!

有一个dart程序包,它为http请求提供了一些帮助程序类.

There is a dart package that provides some helper classes for http requests.

Github: https://github.com/Ephenodrom/Dart-Basic-Utils安装:

Github : https://github.com/Ephenodrom/Dart-Basic-Utils Install it with:

dependencies:
  basic_utils: ^1.5.1

用法

Map<String, String> headers = {
  "Some": "Header"
};
Map<String, String> queryParameters = {
  "Some": "Parameter"
};

String url = "";
String payloadAsString = "{\"date\": \"2019-06-18\"}";

Future<CourseListModel> getCourses() async {

    Map<String, dynamic> body;
    try {
        body = await HttpUtils.postForJson(url, payloadAsString,
        queryParameters: queryParameters, headers: headers);
    } catch (e) {
        // Handle exception, for example if response status code != 200-299
    }
    return CourseListModel.fromJson(body);
}

其他信息:

这些都是HttpUtils类中的所有方法.

These are all methods from the HttpUtils class.

Future<Map<Response> getForFullResponse(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> getForJson(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<String> getForString(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<Map<Response> postForFullResponse(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> postForJson(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> postForString(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Response> putForFullResponse(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> putForJson(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> putForString(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Response deleteForFullResponse(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> deleteForJson(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> deleteForString(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Map<String, dynamic> getQueryParameterFromUrl(String url);
String addQueryParameterToUrl(String url, Map<String, dynamic> queryParameters);

这篇关于将值发布到php文件中以执行sql查询并以json格式获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆