检索数据仅给我数据库的第一行,我只想在数据库中插入最后一行



我正在使用JSON结构从数据库中检索数据。重新检查它仅给了我数据库的第一行,我只想在数据库中插入最后一行。

这是我的java类retireveinfo.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
                +"nOpenDatet"+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);
// }
?>

纠正服务器端 query $ sql =

 SELECT * FROM business_info  ORDER BY column_name DESC LIMIT 1

fyi

column_name应该是主要键。 按关键字订单用于按升或降序顺序排列结果集。desc限制1提取最后一个元素。

只需使用这样的查询:" >(主键( desc limit 1"

select * select * select *。

您需要在主键列上的mysql查询中使用订单。

<?php 
 //if($_SERVER['REQUEST_METHOD']=='GET'){
 //$id  = $_GET['id'];
 require_once('dbConnect.php');
 $sql = "SELECT * from business_info order by primary_key_column desc limit 1";
 $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";    

将其更改为:

$sql = select * from business_info order by yourPrimaryColumn desc limit 1;

最新更新