我正在进行搜索活动,扫描数据库并在android中显示结果。这是我的PHP代码
$keyword = $_POST["keyword"];;
$query = "select * from user where u_name like '%$keyword%' OR u_lname like '%$keyword%' OR u_email like '%$keyword%';";
$result = mysqli_query($con,$query);
$response = array();
while($row = mysqli_fetch_array($result))
{
array_push($response, array("u_id" => $row[0], "u_name" => $row[1],"u_lname" =>$row[2],"u_email" =>$row[3], "u_pw" =>$row[4], "u_dp" => $row[5]));
}
echo json_encode(array("search_response"=>$response));
我让所有用户如何按我的意愿搜索?
这是我的android java代码
HashMap<String, String> params = new HashMap<String, String>();
params.put("keyword", "ka");
JsonObjectRequest req = new JsonObjectRequest("http://ksdb.comlu.com/search.php", new JSONObject(params),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray= response.getJSONArray("search_response");
for(int i = 0; i < jsonArray.length(); i ++){
JSONObject user = jsonArray.getJSONObject(i);
String id = user.getString("u_id");
String name = user.getString("u_name");
String surname = user.getString("u_lname");
String email = user.getString("u_email");
String password = user.getString("u_pw");
String department = user.getString("u_dp");
User newUser = new User(id, name, surname, email, password, department);
userArrayList.add(newUser);
}
setUsersListView(userArrayList );
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
}
});
RequestQueue queue = Volley.newRequestQueue(MainUserPage.this);
queue.add(req);
}
private void updateSchedule() {
User user = userLocalStore.getLoggedInUser();
String key = user.getId();
ArrayList<String> checkedSchedule = checkSchedule();
Response.Listener<String> updateListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
};
UpdateScheduleRequest updateScheduleRequest = new UpdateScheduleRequest(key,checkedSchedule,updateListener);
RequestQueue queue = Volley.newRequestQueue(MainUserPage.this);
queue.add(updateScheduleRequest);
我真的不太了解php我找不到如何解决我的问题。
答案 0 :(得分:0)
我对Java代码不是很聪明,所以我猜:
我没有看到您在哪里指定请求方法,所以我认为您通过GET方法而不是POST发送keyword
值。
然后$_POST['keyword']
始终为空字符串,SQL查询与每个表行匹配。
如果这是原因,您可以将请求方法设置为POST(java代码)或使用$ _GET(php代码)。