根据登录者从Mysql表中选择数据

问题描述 投票:0回答:1

我尝试从 MySQL 表中检索数据到 android 回收器查看我无法根据登录用户检索数据的问题我尝试这个 PHP 代码 我在 POSTMAN 上得到了很好的结果,但是如果我删除了我可以在 POSTMAN 和我的模拟器中得到好的结果的条件,我就没有在我的应用程序中运行任何人都可以帮助我

PHP

<?php
 session_start();
include"config.php";

$stmt = $conn->prepare("SELECT CustomerName,AccountNumber from cases WHERE CollectorName = '{$_SESSION['user_name']}' ");


//WHERE CollectorName = '$CollectorName' 
 //executing the query 
 $stmt->execute();
 //binding results to the query 
 $stmt->bind_result($CustomerName, $AccountNumber);
 $cases = array(); 
 //traversing through all the result 
 while($stmt->fetch()){
$temp = array();
$temp['CustomerName'] = $CustomerName;
$temp['AccountNumber']= $AccountNumber; 
 array_push($cases, $temp);
 }
 //displaying the result in json format 
 echo json_encode($cases);
``your text``> 

android
 private void loadData() {
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url
                , new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONArray array = new JSONArray(response);
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject content = array.getJSONObject(i);
                        mList.add(new ListReports(
                                content.getString("CustomerName"),
                                content.getString("AccountNumber")));
                    }
                    Recycler_ViewAdapter_MyReports adapter = new Recycler_ViewAdapter_MyReports(mList, MyReports.this);
                    recyclerView.setAdapter(adapter);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        })
        ;
        Volley.newRequestQueue(this).add(stringRequest);
    }```

android error
java.lang.RuntimeException: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray
php android mysql android-volley
1个回答
-3
投票

PHP 代码似乎期望设置

$_SESSION['user_name']
值并将其与MySQL 表中的
CollectorName
列相匹配。但是,Android 代码似乎并没有在请求中将此值传递给 PHP 脚本。

您可以修改 Android 代码,通过将登录用户名作为参数添加到

StringRequest
构造函数中的 URL 来将登录用户名发送到 PHP 脚本。例如:

String url = "http://your-php-script-url?username=" + loggedInUsername;

我还附上了经过清理的代码示例,

<?php
session_start();
include "config.php";

// Get the logged-in user's name from the query string
if (!isset($_GET['username'])) {
    // Return an error response if the username parameter is missing
    http_response_code(400);
    echo json_encode(['error' => 'Username parameter is missing']);
    exit;
}
$username = $_GET['username'];

// Prepare the SQL query using parameterized statements
$stmt = $conn->prepare("SELECT CustomerName, AccountNumber FROM cases WHERE CollectorName = ?");
$stmt->bind_param("s", $username);
$stmt->execute();

// Fetch the results and build an array of cases
$cases = [];
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    $cases[] = [
        'CustomerName' => $row['CustomerName'],
        'AccountNumber' => $row['AccountNumber'],
    ];
}

// Return the cases as JSON
header('Content-Type: application/json');
echo json_encode($cases);
© www.soinside.com 2019 - 2024. All rights reserved.