![](/img/trans.png)
[英]Display BLOB image from mysql to android using volley (json encoded)
[英]How to fetch sum of column in MySQL using PHP based on id and display it in Android using Volley
我有一個名為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);
?>
我的Android代碼:
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時沒有錯誤。
在您啟動查詢后,像這樣使用它:
while ($row = mysqli_fetch_assoc($query)) {
$sum=$row["Total"];
echo $sum;
}
查詢后,查看您的總數是否在$ sum中。 之后,您可以在任何地方開始使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.