gpt4 book ai didi

java - 检索数据只给我数据库的第一行,我只想将最后一行插入数据库

转载 作者:行者123 更新时间:2023-11-28 23:16:42 25 4
gpt4 key购买 nike

我正在使用 JSON 结构从数据库中检索数据。检索它只会给我数据库的第一行,而我只想将最后一行插入到数据库中。

这是我的 java 类 RetrieveInfo.java

public class RetrieveInfo extends AppCompatActivity implements View.OnClickListener {

public static final String DATA_URL = "http://ksvira.edu.in/getBusinessDetails.php";
public static final String KEY_NAME = "name";
public static final String KEY_EMAIL = "email";
public static final String KEY_PHONENUM = "phone";
public static final String KEY_ADDRESS = "address";
public static final String KEY_FAX = "fax";
public static final String KEY_MAILADDRESS = "mailaddress";
public static final String KEY_OPENDATE = "opendate";

public static final String JSON_ARRAY = "result";



private Button buttonGet;
private TextView textViewResult;

private ProgressDialog loading;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrieve_info);


buttonGet = (Button) findViewById(R.id.buttonGet);
textViewResult = (TextView) findViewById(R.id.textView);
buttonGet.setOnClickListener(this);
}

private void getData() {

loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);

//String url = DATA_URL+editTextId.getText().toString().trim();
String url=DATA_URL;
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(RetrieveInfo.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});

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

private void showJSON(String response){
String name="";
String email="";
String phonenum ="";
String fax = "";
String address="";
String mailaddress = "";
String opendate = "";

try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(JSON_ARRAY);

int size = jsonObject.length();
JSONObject collegeData = result.getJSONObject(size);
name = collegeData.getString(KEY_NAME);
email=collegeData.getString(KEY_EMAIL);
phonenum=collegeData.getString(KEY_PHONENUM);
fax=collegeData.getString(KEY_FAX);
address = collegeData.getString(KEY_ADDRESS);
mailaddress =collegeData.getString(KEY_MAILADDRESS);
opendate = collegeData.getString(KEY_OPENDATE);

} catch (JSONException e) {
e.printStackTrace();
}
textViewResult.setText("Name:\t"+name+"\nEmail:\t"+email+"\nPhone:\t"+phonenum+"\nFax:\t"+fax+"\nAddress:\t" +address+"\nMailAddress:\t"+mailaddress
+"\nOpenDate\t"+opendate);
}

@Override
public void onClick(View v) {

getData();
}
}

activity_retrieveinfo.xml

<Button
android:text="View Information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button_View1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="30sp"
android:id="@+id/textView2"
android:layout_below="@+id/button_View1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />

getBusinessDetails.php

<?php 

//if($_SERVER['REQUEST_METHOD']=='GET'){

//$id = $_GET['id'];

require_once('dbConnect.php');

$sql = "SELECT * from business_info";

$r = mysqli_query($con,$sql);

// $res = mysqli_fetch_array($r);


$result = array();

while($res = mysqli_fetch_array($r))
{
$result[] = $res;
//array_push($result,array("name"=>$res[0],"email"=>$res[1],"phonenum"=>$res[2],"fax"=>$res[3],"address"=>$res[4],"mailaddress"=>$res[5],"opendate"=>$res[6]));

}

//array_push($result,array("content"=>$res['content']));

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


mysqli_close($con);

// }
?>

最佳答案

纠正您的服务器端查询 $sql =

 SELECT * FROM business_info  ORDER BY column_name DESC LIMIT 1

仅供引用

column_name should be Primary Key . The ORDER BY keyword is used to sort the result-set in ascending or descending order. DESC LIMIT 1 fetching Last Element .

关于java - 检索数据只给我数据库的第一行,我只想将最后一行插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43556225/

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