获得MySQL的PHP​​分钟ID和最大ID值 [英] Get min ID and max ID value in MySQL php

查看:118
本文介绍了获得MySQL的PHP​​分钟ID和最大ID值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何获得的 timeIn 有最小的ID和暂停已经从最大的ID 的MySQL PHP到Android?

这是表work_Details(ID,项目,百分比,timeIn,超时TWD)。现在我想找回 timeIn:12时26分00秒超时11:26:00

在这里输入的形象描述

 公共无效RetrieveTotalHours(最后弦乐ID)//假设ID是69
    {
        类调用getHours扩展的AsyncTask<太虚,太虚,字符串> {
            ProgressDialog负荷;
            @覆盖
            在preExecute保护无效(){
                super.on preExecute();
                装载= ProgressDialog.show(getActivity(),读取......等等......,假的,假的);
            }            @覆盖
            保护无效onPostExecute(String s)将{
                super.onPostExecute(多个);
                loading.dismiss();
                showHours(多个);
            }            @覆盖
            保护字符串doInBackground(虚空...... PARAMS){
                RequestHandler RH =新RequestHandler();
                字符串s = rh.sendGetRequestParam(Configs.RETRIEVE_HOURS,ID);
                返回S;
            }
        }
        调用getHours GE =新调用getHours();
        ge.execute();    }
    私人无效showHours(JSON字符串){
        尝试{
            JSONArray阵列=新JSONArray(JSON);
            的JSONObject的JSONObject = array.getJSONObject(array.length() - 1);
            字符串MiNtimeIn = jsonObject.optString(Configs.TAG_IN);
            字符串MaXtimeOut = jsonObject.optString(Configs.TAG_OUT);
            Log.e(A,MiNtimeIn);
            Log.e(S,MaXtimeOut);
            //total.setText(MiNtimeIn);        }赶上(JSONException E){
            e.printStackTrace();
        }
    }

PHP

 < PHP
  定义(HOST,127.0.0.1:3307');
  定义('用户','根');
  定义('PASS','');
  定义(DB,androiddb');  $ CON = mysqli_connect(HOST,USER,PASS,DB)或死亡(无法连接);  $ TWD = $ _GET ['身份证']; $的SQL =选择timeIn,从work_details WHERE TWD =超时'。$台币。和身份证在
 (SELECT MIN(ID)FROM work_details WHERE TWD =$ TWD。'UNION SELECT MAX(ID)FROM work_details WHERE TWD =$ TWD。');
  $解析度= mysqli_query($ CON,$ SQL);  $结果=阵列();  而($行= mysqli_fetch_array($水库)){
      array_push($结果,阵列('timeIn'=> $行[0],超时= GT; $行[1]));
  } 回声json_en code($结果);mysqli_close($ CON);?>

错误输出

  12月1日至十二日:43:41.540 22692-22692 / com.example.project.myapplication E / A:20时26分00秒
12月1日至12日:43:41.540 22692-22692 / com.example.project.myapplication E / S:11:26:00

据检索它具有最大ID的timeIn和超时...


解决方案

  

获取具有最大ID的timeIn和超时...


做到这一点,以获得 MiNtimeIn 从第一的JSONObject和 MaXtimeOut 从第二JSONObject的:

 字符串MiNtimeIn,MaXtimeOut;
JSONArray阵列=新JSONArray(JSON);
如果(array.length()2){
  的JSONObject的JSONObject = array.getJSONObject(0);
  MiNtimeIn = jsonObject.optString(Configs.TAG_IN);
  MaXtimeOut = jsonObject.optString(Configs.TAG_OUT);
}其他{
  //从JSONArray的第一个对象
   的JSONObject oneObject = array.getJSONObject(0);
  MiNtimeIn = oneObject.optString(Configs.TAG_IN); //从第一行获得分钟
  //从JSONArray第二个对象
   的JSONObject twoObject = array.getJSONObject(array.length() - 1);
   MaXtimeOut = twoObject.optString(Configs.TAG_OUT); //从第二行获得分钟
}

I wonder how to get the timeIn which has the min id and timeOut which has the max id from MySQL php to android ?

This is table work_Details(id,project,percentage,timeIn,timeOut,twd). Now I want to retrieve the timeIn : 12:26:00 and timeOut 11:26:00

  public void RetrieveTotalHours( final String ID)  // Assume ID is 69
    {
        class GetHours extends AsyncTask<Void,Void,String> {
            ProgressDialog loading;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(getActivity(),"Fetching...","Wait...",false,false);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                showHours(s);
            }

            @Override
            protected String doInBackground(Void... params) {
                RequestHandler rh = new RequestHandler();
                String s = rh.sendGetRequestParam(Configs.RETRIEVE_HOURS,ID);
                return s;
            }
        }
        GetHours ge = new GetHours();
        ge.execute();

    }
    private void showHours(String json) {
        try {
            JSONArray array=new JSONArray(json);
            JSONObject jsonObject = array.getJSONObject(array.length()-1);
            String MiNtimeIn = jsonObject.optString(Configs.TAG_IN);
            String MaXtimeOut=jsonObject.optString(Configs.TAG_OUT);
            Log.e("A",MiNtimeIn);
            Log.e("S", MaXtimeOut);
            //total.setText(MiNtimeIn);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

php

<?php
  define('HOST','127.0.0.1:3307');
  define('USER','root');
  define('PASS','');
  define('DB','androiddb');

  $con = mysqli_connect(HOST,USER,PASS,DB) or die('unable to connect');

  $twd= $_GET['id'];

 $sql = "select timeIn, timeOut from work_details WHERE twd = '".$twd."' AND id IN
 (SELECT MIN(id) FROM work_details WHERE twd ='".$twd."' UNION SELECT MAX(id) FROM work_details WHERE twd='".$twd."')";


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

  $result=array();

  while($row=mysqli_fetch_array($res)){
      array_push($result,array('timeIn'=>$row[0],'timeOut'=>$row[1]));
  }

 echo json_encode($result);

mysqli_close($con);

?>

Wrong Output

01-12 12:43:41.540  22692-22692/com.example.project.myapplication E/A﹕ 20:26:00
01-12 12:43:41.540  22692-22692/com.example.project.myapplication E/S﹕ 11:26:00

It retrieves the timeIn and timeOut which has the max id...

解决方案

retrieves the timeIn and timeOut which has the max id...

Do it as to get MiNtimeIn from First JSONObject and MaXtimeOut from second JSONObject:

String MiNtimeIn,MaXtimeOut;
JSONArray array=new JSONArray(json);
if(array.length()<2){
  JSONObject jsonObject = array.getJSONObject(0);
  MiNtimeIn = jsonObject.optString(Configs.TAG_IN);
  MaXtimeOut=jsonObject.optString(Configs.TAG_OUT);
}else{
  // get First Object from JSONArray
   JSONObject oneObject = array.getJSONObject(0);
  MiNtimeIn = oneObject.optString(Configs.TAG_IN); // get min from first row
  // get Second Object from JSONArray
   JSONObject twoObject = array.getJSONObject(array.length()-1);
   MaXtimeOut = twoObject.optString(Configs.TAG_OUT); // get min from second row
}

这篇关于获得MySQL的PHP​​分钟ID和最大ID值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆