gpt4 book ai didi

php - 如何使用Volley通过JSON从mysql获取多行数据

转载 作者:行者123 更新时间:2023-11-29 11:18:43 25 4
gpt4 key购买 nike

我正在尝试通过 JSON 将下面提到的 php 文件的输出获取到我的 Android 应用程序中,但我只得到我的 Android 应用程序中的第一个条目。我也尝试执行以下步骤,但我能够仅获取第一个输出,但其余输出在我的应用程序中不可见

<?php 
define('HOST','XXXX');
define('USER','XXXX');
define('PASS','XXXX');
define('DB','XXXX');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
if($_SERVER['REQUEST_METHOD']=='GET'){
$customerEmail = $_GET['customerEmail'];
$sql = "SELECT `amount`, `customerEmail`, `CCAvenueOrder_id` FROM ` OrderAborted` WHERE customerEmail='".$customerEmail."'";
$r = mysqli_query($con,$sql);
while($res = mysqli_fetch_array($r))
{
$result = array();
array_push($result,array(
"customerEmail"=>$res['customerEmail'],
"CCAvenueOrder_id"=>$res['CCAvenueOrder_id'],
"amount"=>$res['amount']
)
);
echo json_encode(array("result"=>$result));
}
mysqli_close($con);
}
?>

JSON 代码

     private void getData() {
String id = editTextId.getText().toString().trim();
if (id.equals("")) {
Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
return;
}
loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);

String url = Constants.DATA_URL+editTextId.getText().toString().trim();

StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(OrderHistory.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}

private void showJSON(String response){
String name="";
String address="";
String vc = "";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Constants.JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(0);
name = collegeData.getString(Constants.KEY_NAME);
address = collegeData.getString(Constants.KEY_ADDRESS);
vc = collegeData.getString(Constants.KEY_VC);
} catch (JSONException e) {
e.printStackTrace();
}
textViewResult.setText("Name:\t"+name+"\nAddress:\t" +address+ "\nVice Chancellor:\t"+ vc);
}

最佳答案

很简单,在完成循环之前不要发送数据,并将数组初始化移到循环之外

<?php 
define('HOST','XXXX');
define('USER','XXXX');
define('PASS','XXXX');
define('DB','XXXX');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
if($_SERVER['REQUEST_METHOD']=='GET'){
$customerEmail = $_GET['customerEmail'];
$sql = "SELECT `amount`, `customerEmail`, `CCAvenueOrder_id` FROM `OrderAborted` WHERE customerEmail='".$customerEmail."'";
$r = mysqli_query($con,$sql);

$result = array();
while($res = mysqli_fetch_array($r))
{
//$result = array();
$result[] = array(
"customerEmail"=>$res['customerEmail'],
"CCAvenueOrder_id"=>$res['CCAvenueOrder_id'],
"amount"=>$res['amount']
);
//echo json_encode(array("result"=>$result));
}

echo json_encode(array("result"=>$result));
mysqli_close($con);
}
?>

如果您使用mysqli_fetch_assoc()而不是mysqli_fetch_array(),您还可以简化循环,并准确地从结果集中获取手动构建的数组。

<?php 
define('HOST','XXXX');
define('USER','XXXX');
define('PASS','XXXX');
define('DB','XXXX');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
if($_SERVER['REQUEST_METHOD']=='GET'){
$customerEmail = $_GET['customerEmail'];
$sql = "SELECT `amount`, `customerEmail`, `CCAvenueOrder_id` FROM `OrderAborted` WHERE customerEmail='".$customerEmail."'";
$r = mysqli_query($con,$sql);

$result = array();
while($res = mysqli_fetch_assoc($r))
{
$result[] = $res;
//echo json_encode(array("result"=>$result));
}

echo json_encode(array("result"=>$result));
mysqli_close($con);
}
?>

如果您使用mysqli_fetch_all(),甚至可以再次简化它

<?php 
define('HOST','XXXX');
define('USER','XXXX');
define('PASS','XXXX');
define('DB','XXXX');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
if($_SERVER['REQUEST_METHOD']=='GET'){
$customerEmail = $_GET['customerEmail'];
$sql = "SELECT `amount`, `customerEmail`, `CCAvenueOrder_id` FROM `OrderAborted` WHERE customerEmail='".$customerEmail."'";
$r = mysqli_query($con,$sql);

$result = mysqli_fetch_all($r, MYSQLI_ASSOC);

echo json_encode(array("result"=>$result));
}
?>

And to get rid of the SQL Injection vector

<?php 
define('HOST','XXXX');
define('USER','XXXX');
define('PASS','XXXX');
define('DB','XXXX');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
if($_SERVER['REQUEST_METHOD']=='GET'){
//$customerEmail = $_GET['customerEmail'];

$sql = "SELECT `amount`, `customerEmail`, `CCAvenueOrder_id`
FROM `OrderAborted`
WHERE customerEmail=?";

$stmt = mysqli_prepare($con,$sql);

$stmt = mysqli_stmt_bind_param ($stmt, 's', $_GET['customerEmail']);

$status = mysqli_execute($stmt);

$r = mysqli_stmt_get_result ($stmt);

$result = mysqli_fetch_all($r, MYSQLI_ASSOC);

echo json_encode(array("result"=>$result));
}
?>

关于php - 如何使用Volley通过JSON从mysql获取多行数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39331696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com