简体   繁体   English

使用php和android从sql数据库中选择所有内容

[英]Selecting everything from sql database with php and android

https://www.dropbox.com/s/1uq36sbd6d7a3gr/Untitled.png?dl=0 https://www.dropbox.com/s/1uq36sbd6d7a3gr/Untitled.png?dl=0

I am connecting to my .php file through my android app which then connects to my sql database, it then grabs all the info I have saved there by receiving a username and getting all info with the same username and displays it in a recycler viewer. 我通过我的android应用程序连接到我的.php文件,然后连接到我的sql数据库,然后它通过接收用户名并获取具有相同用户名的所有信息来获取我保存在那里的所有信息,并将其显示在回收查看器中。 So far it's only showing me the last pet. 到目前为止,它只是向我显示了最后一只宠物。 How do I tell my code to show ALL the pets under that username. 如何告诉我的代码显示该用户名下的所有宠物。

JAVA CODE JAVA代码

**RECEIVE PET**
public class ReceivePet extends StringRequest {

private static final String RECEIVE_PET_URL = "http://lassie.netai.net/ReceivePet.php";
private Map<String, String> params;

public ReceivePet (String username, Response.Listener<String> listener) {
    super(Request.Method.POST, RECEIVE_PET_URL, listener, null);
    params = new HashMap<>();
    params.put("username", username);
}

@Override
public Map<String, String> getParams() {
    return params;
}

} }

**WHERE I GET MY SQL DATABASE INFO**
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) {
                    petName = jsonResponse.getString("petname");
                    petBreed = jsonResponse.getString("petbreed");
                    phoneNumber = jsonResponse.getString("phonenumber");
                    petDescription = jsonResponse.getString("petdescription");
                    imageUrl = jsonResponse.getString("imageurl");

                    petsList.add(new Pet(petName, petBreed, phoneNumber, petDescription, imageUrl));
                    adapter.notifyDataSetChanged();
                } else {

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };
    ReceivePet receiverPetRequest = new ReceivePet(username, responseListener);
    RequestQueue queue = Volley.newRequestQueue(getActivity());
    queue.add(receiverPetRequest);

PHP CODE PHP代码

<?php
$con = mysqli_connect("mysql13.000webhost.com", "a9017958_omer", "pass123", "a9017958_users");

$username = $_POST["username"];

$statement = mysqli_prepare($con, "SELECT * FROM pets WHERE username = ?");
mysqli_stmt_bind_param($statement, "s", $username);
mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $petsID, $username, $petname, $petbreed, $phonenumber, $petdescription, $imageurl);

$response = array();
$response["success"] = false;

while(mysqli_stmt_fetch($statement)) {
    $response["success"] = true;
    $response["petname"] = $petname;
    $response["petbreed"] = $petbreed;
    $response["phonenumber"] = $phonenumber;
    $response["petdescription"] = $petdescription;
    $response["imageurl"] = $imageurl;
}

print_r (json_encode($response));

?> ?>

In PHP in the while loop, you're overwriting the values in the $response at each iteration. PHPwhile循环中,您每次迭代都会覆盖$response中的值。 You need to create an array of arrays. 您需要创建一个数组数组。 Here is an example: 这是一个例子:

$response = array();
while(mysqli_stmt_fetch($statement)) {
    $entry = array();
    $entry["success"] = true;
    $entry["petname"] = $petname;
    $entry["petbreed"] = $petbreed;
    $entry["phonenumber"] = $phonenumber;
    $entry["petdescription"] = $petdescription;
    $entry["imageurl"] = $imageurl;
    $response[] = $entry;
}

Also, you should use echo json_encode($response); 另外,您应该使用echo json_encode($response); instead of print_r(json_encode($response)); 而不是print_r(json_encode($response)); .

And in Java, since now you're getting a JSON array as a response. 在Java中,从现在开始,您将获得一个JSON数组作为响应。 You need to change: 您需要更改:

JSONObject jsonResponse = new JSONObject(response);

to: 至:

JSONArray jsonResponse = new JSONArray(response);

and loop through this array to process all the results. 并遍历此数组以处理所有结果。

for(int i = 0; i < jsonResponse.length(); i++){
    JSONObject obj = jsonResponse.getJSONObject(i);
    petsList.add(new Pet(obj.getString("petname"), obj.getString("petbreed"),....));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM