【问题标题】:Retrieving data only gives me first row of the database and i only want the last row inserted in the database检索数据只给我数据库的第一行,我只希望将最后一行插入数据库
【发布时间】:2017-09-19 06:05:55
【问题描述】:

我正在使用 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);

// }
?>

【问题讨论】:

  • 如果没有 ORDER BY 子句,像 first 和 last 这样的概念就毫无意义
  • 非常感谢您的帮助。

标签: java php android mysql json


【解决方案1】:

只需像这样使用查询:“SELECT * from business_info order by (primary key) desc limit 1”

【讨论】:

    【解决方案2】:

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

     SELECT * FROM business_info  ORDER BY column_name DESC LIMIT 1
    

    仅供参考

    column_name 应该是 Primary Key 。 ORDER BY 关键字用于按升序或降序对结果集进行排序。 DESC LIMIT 1 获取最后一个元素。

    【讨论】:

    • 我应该把id列作为主键吗?
    • @YashKaushik 是的。让我通知
    【解决方案3】:

    您需要在主键列的 mysql 查询中使用 order by。

    <?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);
    
    // }
    ?>
    

    【讨论】:

    • 我应该将 id 作为表中的主键
    • @Yash Kaushik 是的,你可以。
    【解决方案4】:

    在你的:

    $sql = "SELECT * from business_info";    
    

    改成:

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

    【讨论】:

    • 实际上先生,我是 android 编程的新手,这就是我遇到问题的原因?
    • 我应该在我的数据库表中创建一个列 id 并将其作为主键
    • 是的,至少你的表中必须有一个唯一的主键列
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多