2016-01-03 73 views
1

我想通過一個字符串(例如「名稱」)到我的PHP腳本,它需要從$ _POST獲取Android應用程序的「名稱」,然後使用它一個SELECT查詢並將其傳回給Android應用程序。傳遞字符串到PHP文件來查詢SELECT語句

我有一切,除了我只能傳遞字符串到PHP腳本,但我不能在SELECT查詢中使用它。這裏是我的代碼:

PHP代碼:

<?php 
include "../dbc.php"; 
//include "get_id.php"; 

$value = $_POST["value"]; 
$response = array(); 

$q=$dbh->prepare("SELECT * FROM users WHERE user_name = :value"); 
$q->bindParam(':value', $value); 
$q->execute(); 
while ($row = $q->fetch(PDO::FETCH_ASSOC)) 
{ 
    $first_name=$row['first_name']; 
    $last_name=$row['last_name']; 
    $company_name=$row['company_name']; 
    $loc=$row['loc']; 
    $tel=$row['tel']; 
    $website=$row['website']; 
    $aboutme=$row['aboutme']; 
    array_push($response, array("first_name"=>$first_name,"last_name"=>$last_name,"company_name"=>$company_name,"loc"=>$loc,"tel"=>$tel,"website"=>$website,"aboutme"=>$aboutme)); 
} 

echo json_encode(array("server_response"=>$response)); 
?> 

的Android代碼:

class GetUserSettings extends AsyncTask<Void,Void,String> { 
    String json_get_settings_url; 
    @Override 
    protected void onPreExecute() { 
     json_get_settings_url = "http://url.com/json.php"; 
    } 

    @Override 
    protected void onProgressUpdate(Void... values) { 
     super.onProgressUpdate(values); 
    } 

    @Override 
    protected String doInBackground(Void... Voids) { 
     URL url = null; 
     try { 
      url = new URL(json_get_settings_url); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
     try { 
      HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); 
      InputStream inputStream = httpURLConnection.getInputStream(); 
      BufferedReader bufferedReader = new BufferedReader (new InputStreamReader(inputStream)); 
      StringBuilder stringBuilder = new StringBuilder(); 
      while((JSON_STRING = bufferedReader.readLine()) != null) { 
       stringBuilder.append(JSON_STRING+"\n"); 
      } 
      bufferedReader.close(); 
      inputStream.close(); 
      httpURLConnection.disconnect(); 
      return stringBuilder.toString().trim(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
    @Override 
    protected void onPostExecute(String JSON_STRING) { 
     SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); 
     String value = settings.getString("user_name", ""); 
     //this is where it starts parsing the JSON from the URL 
     try { 
      JSONObject jsonResponse = new JSONObject(JSON_STRING); 
      JSONArray jsonMainNode = jsonResponse.optJSONArray("server_response"); 

      for (int i = 0; i < jsonMainNode.length(); i++) { 
       JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); 
       user_name = jsonChildNode.optString(value); 
       et_user_name.setText(value); 
       first_name = jsonChildNode.optString("first_name"); 
       et_first_name.setText(first_name); 
       last_name = jsonChildNode.optString("last_name"); 
       et_last_name.setText(last_name); 
       company_name = jsonChildNode.optString("company_name"); 
       et_company_name.setText(company_name); 
       loc = jsonChildNode.optString("loc"); 
       et_loc.setText(loc); 
       tel = jsonChildNode.optString("tel"); 
       et_tel.setText(tel); 
       website = jsonChildNode.optString("website"); 
       et_website.setText(website); 
       aboutme = jsonChildNode.optString("aboutme"); 
       et_aboutme.setText(aboutme); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
      Toast.makeText(getApplicationContext(),"Something is wrong. Please restart the app or contact us.",Toast.LENGTH_LONG).show(); 
     } 
    } 
} 
從代碼

所以,你可以看到,我試圖通過字符串 「價值」我的PHP代碼,然後分配「價值」$ value,然後在SELECT查詢中使用$值,但我怎麼能傳遞字符串「價值」,然後執行查詢,所以我可以使用JSON來解析數據?

+1

你的Android代碼不會發布任何東西到你的PHP腳本。所以$ _POST數組是空的。開始調整你的php代碼來檢查是否需要參數/數據。如果沒有將相關信息回顯給Android客戶端。 – greenapps

回答

0

您可以使用ContentValues()將字符串傳遞給php amd並返回select查詢。

ContentValues values = new Content Values(); values.put(「string」,variable);

而且你可以通過URL作爲

http://example.com/dir/page.php?var=variable

在PHP端

是$ var = $ _GET [ '變量']: 現在你可以在選擇查詢通過這個$變種。

希望這會有所幫助。