如何从数据库中获取当前日期,最近7、30和6个月的数据并在android中以json格式显示

时间:2019-05-16 09:55:24

标签: php mysql json

在我的应用程序中,我想获取最近几天的数据并显示在列表中,但是我不知道如何获取和显示它。 我使用这些数据并将其转换为json格式,以便可以轻松将其访问到我的应用程序中。

<?php

require "dbconfig.php";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

 //Checking if any error occured while connecting
 if (mysqli_connect_errno()) {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
 die();
 }

 //creating a query
 $stmt = $conn->prepare("SELECT *, (CURDATE() - INTERVAL 7 DAY) AS diff
                        FROM register 
                        WHERE date  >= CURDATE() - INTERVAL 7 DAY
                        ORDER BY date DESC;");

 //executing the query 
 $stmt->execute();

 //binding results to the query 
 $stmt->bind_result($id, $name, $amount, $date);

 $register = array(); 

 //traversing through all the result 
 while($stmt->fetch()){
 $temp = array();
 $temp['id'] = $id; 
 $temp['name'] = $name; 
 $temp['amount'] = $amount; 
 $temp['date'] = $date; 

 array_push($register, $temp);
 }

 //displaying the result in json format 
 echo json_encode($register);
?>

当前我使用上述代码,但无法正常运行并显示记录

0 个答案:

没有答案