gpt4 book ai didi

php - android 如何通过选定的id获取值

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

下面的代码android代码从mysql获取数据并显示其中通过id获取的所有值。但我想转换代码而不是获取所有数据 id 让它只获取选定的 comment_id = 3 它们只会显示 comment _id 3 的所有相关评论的值

id             comment_id                  comment
1 1 love this product
2 1 not bad
3 2 too expensive
4 2 how much
5 2 too old
6 3 not that new
7 5 hate it
8 7 go away
9 8 what wrong with you
10 4 hi
11 7 hi
12 4 helo
13 7 hola
14 4 go away
15 6 nice
16 3 bye

这就是 Android 代码

package com.androidjson.serverupdate_androidjsoncom;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class test2 extends AppCompatActivity {

ListView StudentListView;
ProgressBar progressBar;
String HttpUrl = "http://192.168.0.199/edit/AllStudentData.php";
List<String> IdList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_show_all_students);

StudentListView = (ListView)findViewById(R.id.listview1);

progressBar = (ProgressBar)findViewById(R.id.progressBar);

new GetHttpResponse(test2.this).execute();

//Adding ListView Item click Listener.
StudentListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

// TODO Auto-generated method stub

Intent intent = new Intent(test2.this,ShowSingleRecordActivity.class);

// Sending ListView clicked value using intent.
intent.putExtra("ListViewValue", IdList.get(position).toString());

startActivity(intent);

//Finishing current activity after open next activity.
finish();

}
});
}

// JSON parse class started from here.
private class GetHttpResponse extends AsyncTask<Void, Void, Void> {
public Context context;

String JSonResult;

List<Student> studentList;

public GetHttpResponse(Context context)
{
this.context = context;
}

@Override
protected void onPreExecute()
{
super.onPreExecute();
}

@Override
protected Void doInBackground(Void... arg0)
{
// Passing HTTP URL to HttpServicesClass Class.
HttpServicesClass httpServicesClass = new HttpServicesClass(HttpUrl);
try
{
httpServicesClass.ExecutePostRequest();

if(httpServicesClass.getResponseCode() == 200)
{
JSonResult = httpServicesClass.getResponse();

if(JSonResult != null)
{
JSONArray jsonArray = null;

try {
jsonArray = new JSONArray(JSonResult);

JSONObject jsonObject;

Student student;

studentList = new ArrayList<Student>();

for(int i=0; i<jsonArray.length(); i++)
{
student = new Student();

jsonObject = jsonArray.getJSONObject(i);

// Adding Student Id TO IdList Array.
IdList.add(jsonObject.getString("id").toString());

//Adding Student Name.
student.StudentName = jsonObject.getString("name").toString();

studentList.add(student);

}
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else
{
Toast.makeText(context, httpServicesClass.getErrorMessage(), Toast.LENGTH_SHORT).show();
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void result)

{
progressBar.setVisibility(View.GONE);

StudentListView.setVisibility(View.VISIBLE);

ListAdapterClass adapter = new ListAdapterClass(studentList, context);

StudentListView.setAdapter(adapter);

}
}
}

这是我的 php 代码

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){

include_once($_SERVER['DOCUMENT_ROOT']."/config/config.php");

$id= $_POST['id'];

// Create connection
$conn = new mysqli($host, $user, $pass, $database);

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM comments where comment_id = '$id'" ;

$result = $conn->query($sql);

if ($result->num_rows >0) {


while($row[] = $result->fetch_assoc()) {

$tem = $row;

$json = json_encode($tem);

}

} else {
echo "No Results Found.";
}
echo $json;

$conn->close();
}
?>

最佳答案

你应该发送 id 并将其添加到你的 mysql WHERE 中。它可能有用: link

关于php - android 如何通过选定的id获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46516887/

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