我想通过一个字符串(例如“名称”)到我的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来解析数据?
你的Android代码不会发布任何东西到你的PHP脚本。所以$ _POST数组是空的。开始调整你的php代码来检查是否需要参数/数据。如果没有将相关信息回显给Android客户端。 – greenapps