2016-01-01 188 views
1

我很好奇如何使用WHERE子句从MySQL获取数据并最终加载到android listView?我想获得日期,timeIntimeOut根据名称和月份。这是我到目前为止所尝试的。使用where子句检索MySQL数据

的GetData

public void getData(String name, String month) { 
     class GetDataJSON extends AsyncTask<String, Void, String> { 

      @Override 
      protected String doInBackground(String... params) { 
       DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); 
       HttpPost httppost = new HttpPost("http://192.168.1.7/Android/CRUD/retrieveInformation.php"); 


       // Depends on your web service 
       httppost.setHeader("Content-type", "application/json"); 

       InputStream inputStream = null; 
       String result = null; 
       try { 
        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity(); 

        inputStream = entity.getContent(); 
        // json is UTF-8 by default 
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 
        StringBuilder sb = new StringBuilder(); 

        String line = null; 
        while ((line = reader.readLine()) != null) 
        { 
         sb.append(line + "\n"); 
        } 
        result = sb.toString(); 
       } catch (Exception e) { 
        // Oops 
       } 
       finally { 
        try{if(inputStream != null)inputStream.close();}catch(Exception squish){} 
       } 
       return result; 
      } 

      @Override 
      protected void onPostExecute(String result){ 
       myJSON=result; 
       showList(); 
      } 
     } 
     GetDataJSON g = new GetDataJSON(); 
     g.execute(); 
    } 

    protected void showList(){ 
     try { 
      JSONObject jsonObj = new JSONObject(myJSON); 
      information = jsonObj.getJSONArray(Config.TAG_RESULTS); 

      for(int i=0;i<information.length();i++){ 
       JSONObject c = information.getJSONObject(i); 
       String date = c.getString(Config.TAG_DATE); 
       String timeIn = c.getString(Config.TAG_TiME_IN); 
       String timeOut = c.getString(Config.TAG_TIME_OUT); 
       HashMap<String,String> info = new HashMap<String,String>(); 
       info.put(Config.TAG_DATE, date); 
       info.put(Config.TAG_TiME_IN, timeIn); 
       info.put(Config.TAG_TIME_OUT,timeOut); 

       infoList.add(info); 
      } 

      ListAdapter adapter = new SimpleAdapter(
        HomePage.this, infoList, R.layout.retrieve_data, 
        new String[]{Config.TAG_DATE,Config.TAG_TiME_IN,Config.TAG_TIME_OUT}, 
        new int[]{R.id.date,R.id.timeIn,R.id.timeOut} 
      ); 

      listView.setAdapter(adapter); 

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

    } 

retrieveInformation.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'); 

    $name = $_GET['name']; 

    $month = $_GET['month']; 

    $sql = "select * from information WHERE name= '". $name."' and month = '".$month."'"; 

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

    $result=array(); 

    while($row=mysqli_fetch_array($res)){ 
     array_push($result,array('id'=>$row[0],'name'=>$row[1],'weather'=>$row[2],'date'=>$row[3],'status'=>$row[4], 
     'time_in'=>$row[5], 'time_out'=>$row[6])); 
    } 

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

mysqli_close($con); 

?> 

我能够从MySQL通过 使用我已经张贴在代码检索所有数据到安卓listView的GetData。现在如何根据名称和月份检索 数据?我找不到 谷歌的任何教程...

回答

2

它看起来像你想要的名称/月作为参数传递给你的PHP文件。 你可以做最简单的方法是通过在URL字符串传递参数(简单的方法):

HttpPost httppost = new HttpPost('http://192.168.1.7/Android/CRUD/retrieveInformation.php?name=SomeName&month=SomeMonth') 

然后,名称和月份被看作是你在你的PHP文件中有$ _GET变量。

但是,因为你希望你的代码进行编码正确,你会做这样的事情,而不是:

URI uri = new URIBuilder() 
    .setScheme("http") 
    .setHost("192.168.1.7") 
    .setPath("/Android/CRUD/retrieveInformation.php") 
    .addParameter("name", name) 
    .addParameter("month", month) 
    .build(); 
HttpGet httpget = new HttpGet(uri); 

注意,我在第二个例子中使用HttpGet而不是HttpPost

快速在你的PHP代码的建议,所以你不必所有的索引重新映射到它们的键名:

while($row=mysqli_fetch_assoc($res)){ 
    $result[] = $row; 
} 

mysqli_fetch _assoc会将数组的键设置为列名。这将发送所有的列。如果你不想将所有列,只有7列,您应该修改您的选择查询到这一点:

$sql = "select id, name, weather, date, status, time_in, time_out from information WHERE name= '". $name."' and month = '".$month."'"; 

而且,你的选择查询之前也消毒$ name和$月:

$name = mysqli_escape_string($name); 
$month = mysqli_escape_string($month); 

这里是你的代码更新,以反映修改:

<?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'); 

$name = $_GET['name']; 

$month = $_GET['month']; 
$months = [ 
'January' => '01', 
'February' => '02', 
'March' => '03', 
'April' => '04', 
'May' => '05', 
'June' => '06', 
'July' => '07', 
'August' => '08', 
'September' => '09', 
'October' => '10', 
'November' => '11', 
'December' => '12', 
]; 

if (!isset($months[ $month ])) { 
    die("Invalid month"); 
} 

$month = $months[ $month ]; 


$name = mysqli_escape_string($con, $name); 
$month = mysqli_escape_string($con, $month); 
$sql = "select * from information WHERE name= '". $name."' and month = '".$month."'"; 

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

$result=array(); 

while($row=mysqli_fetch_assoc($res)){ 
    $result[] = $row; 
} 

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

mysqli_close($con); 

?> 
+0

我应该在哪里添加上面的代码? –

+0

你会替换你有我的第二个代码段定义的'httppost'变量 – Clay

+0

我的php看起来是否正确? –

3

您没有在您的HTTP请求中放入名称和月份。

试试这个

HttpPost httppost = new HttpPost("http://192.168.1.7/Android/CRUD/retrieveInformation.php?name="+name+"&month="+month); 

希望这有助于:)

+0

我是否需要更改/添加在'getData'什么? –

+0

我不这么认为。如果您更改了网址,它可能会有效。 –

+1

@John Joe:叶敏Htut(正确!)说如果你要在你的PHP中做一个'$ _GET ['name'];'......那么你最好在URL中有一个参数'name = xyz'。与“月”相同:这两个参数都需要在URL中指定。还有:看看克莱顿的回应。他说的是同样的事情。 Clayton也(正确的)指出你可能希望使用'URIBuilder',并且你可以使用'HttpGet()'而不是“HttpPost()”。 – paulsm4