我正在使用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);
// }
?>
答案 0 :(得分:1)
纠正服务器端查询 $ sql =
SELECT * FROM business_info ORDER BY column_name DESC LIMIT 1
FYI
column_name应该是主键。 ORDER BY关键字用于按升序或降序对结果集进行排序。 DESC LIMIT 1获取最后一个元素。
答案 1 :(得分:0)
只需使用如下查询:“SELECT * from business_info order by (主键) desc limit 1”
答案 2 :(得分:0)
您需要在主键列的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);
// }
?>
答案 3 :(得分:0)
在你的:
$sql = "SELECT * from business_info";
将其更改为:
$sql = select * from business_info order by yourPrimaryColumn desc limit 1;
答案 4 :(得分:0)
问题是无法识别插入的LAST记录是什么。通过主键对结果进行排序仅在与插入顺序有关时才有效。因此,如果您的主键是例如&#39; name&#39;,这与插入的最后一行有什么关系?!
如果您将ID列(例如自动增量字段)作为主键,这将有效。