如何从phpmyadmin数据库获取所有行并在Android中显示它们?
当我得到一行时,一切都很好。当我尝试读取所有行时,我只得到最后一行。
我的php:
<?php
$con = mysqli_connect("localhost", "id831385_rootroot", "rootroot", "id831385_bukrisk");
$accept= $_POST["accept"];
$statement = mysqli_prepare($con, "SELECT * FROM payments WHERE accept= ?");
mysqli_stmt_bind_param($statement, "i", $accept);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $id_pay, $id_user, $value, $accept);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["id_pay"] = $id_pay;
$response["id_user"] = $id_user;
$response["value"] = $value;
$response["accept"] = $accept;
}
echo json_encode($response);
?>
我的java请求:
public class GetPaymentsRequest extends StringRequest {
private static final String LOGIN_REQUEST_URL = "https://pacinho.000webhostapp.com/PaymentsList.php";
private Map<String, String> params;
public GetPaymentsRequest(String accept, Response.Listener<String> listener) {
super(Request.Method.POST, LOGIN_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("accept", accept);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
我在活动中的java代码:
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
int id_pay = jsonResponse.getInt("id_pay");
int id_user = jsonResponse.getInt("id_user");
int value = jsonResponse.getInt("value");
//Toast.makeText(myView.getContext(),""+id_pay, Toast.LENGTH_SHORT).show();
String id_payS = String.valueOf(id_pay);
String id_userS = String.valueOf(id_user);
String valueS = String.valueOf(value);
showPay.setText(showPay.getText() + " " + id_payS);
//UserPayments UP = new UserPayments(id_payS,id_userS,valueS);
//paymentsList.add(UP);
//paymentsCustomAdapter.notifyDataSetChanged();
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
GetPaymentsRequest paymentsRequest = new GetPaymentsRequest("0", responseListener);
RequestQueue queue = Volley.newRequestQueue(myView.getContext());
queue.add(paymentsRequest);
答案 0 :(得分:0)
对于每行,您获取PHP代码只是覆盖$response
的值。您应该创建一个数组并将所有行放入其中
while(mysqli_stmt_fetch($statement)){
$payment = array();
$payment["success"] = true;
$payment["id_pay"] = $id_pay;
$payment["id_user"] = $id_user;
$payment["value"] = $value;
$payment["accept"] = $accept;
$response[] = $payment;
}
您还需要修改android端的代码来处理数组。