【问题标题】:How to fetch sum of column in MySQL using PHP based on id and display it in Android using Volley如何使用基于 id 的 PHP 获取 MySQL 中的列总和并使用 Volley 在 Android 中显示
【发布时间】:2019-01-15 10:04:08
【问题描述】:

我有一个名为hotel_review 的表,我有一个hotel_stars 列,我想使用Volley 显示Android 中所有酒店星级的总和。我想显示酒店评级的平均值,为此我需要列的总和。我想我很接近但没有得到价值观。我没有收到任何错误,但我也无法获取值。

我的php代码:

    <?php
require "init.php";

header('Content-Type: application/json;charset:UTF-8');

$hotel_id = $_POST['hotel_id'];

$query = mysqli_query($con, "SELECT SUM(hotel_stars) AS 'Total' FROM hotel_review WHERE hotel_id = '".$hotel_id."';");
$row = mysqli_fetch_assoc($query);
$sum = $row['Total'];

if($query)
{
   $code = "getvalue";
   $data[]['Total'] = $sum;
   $result = array("code"=>$code,"stars" => $data);
   echo json_encode($result);

}else {

    $code = "not_get";
    $msg = "Try Again....";
    array_push($response, array("code"=>$code,"msg"=>$msg));
     //echo("Error description: " . mysqli_error($con));
    header('Content-Type: application/json;charset:UTF-8');
    echo json_encode($response);
     // echo("Error description: " . mysqli_error($con));
}
mysqli_close($con);
?>

我的安卓代码:

public void getHotelStars()
    {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, getStars,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("response", response);
                        JSONArray jsonArray = null;
                        try {
                            jsonArray = new JSONArray(response);
                            JSONObject jsonObject = jsonArray.getJSONObject(0);
                            String code = jsonObject.getString("code");
                            if (code.equals("not_get")) {
                                Toast.makeText(hotel_reviews.this, "error", Toast.LENGTH_SHORT).show();

                            } else {
                                String totalStars = jsonObject.getString("stars");
//                                String totalCount = jsonObject.getString("total_stars");
                                Toast.makeText(hotel_reviews.this, ""+totalStars, Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(hotel_reviews.this, "error", Toast.LENGTH_SHORT).show();
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("hotel_id",HotelId);
                //params.put("division",EmployeeDivision);
                return params;
            }
        };MySingleton.getInstance(hotel_reviews.this).addToRequestque(stringRequest);
    }

我在 params.put 中获得了正确的 hotel_id,所以在获取 id 时没有错误。

【问题讨论】:

  • 服务器的响应是什么?你和 Postman 核对过吗?
  • 我一无所获
  • 做一件事,在执行查询后请检查它是否成功或有问题。如果查询成功,还检查查询是否返回任何内容。

标签: java php android


【解决方案1】:

在触发查询后像这样使用它:

while ($row = mysqli_fetch_assoc($query)) {
    $sum=$row["Total"];
    echo $sum;
}

在您查询之后,看看您的总数是否以 $sum 为单位。此后,您可以在任何地方开始使用它。

【讨论】:

  • 其他一切似乎都还好?
  • @RajBedi 我只是指出如何从查询中立即获得总数。其他代码的逻辑看起来不错,但你现在必须尝试一下。在一个项目上工作时,小错误会引起所有程序员的注意。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
  • 2019-04-05
  • 2019-08-26
  • 2021-12-11
相关资源
最近更新 更多