将字符串传递到PHP文件以查询SELECT语句
我试图将一个字符串(例如“name”)传递给我的PHP脚本,该脚本需要通过$u POST从Android应用程序中获取“name”,然后在选择查询中使用该字符串并将其传递回Android应用程序 除了只能将字符串传递给PHP脚本之外,我已经完成了所有操作,但是我不能在SELECT查询中使用它。这是我的密码: PHP代码:将字符串传递到PHP文件以查询SELECT语句,php,android,mysql,arrays,json,Php,Android,Mysql,Arrays,Json,我试图将一个字符串(例如“name”)传递给我的PHP脚本,该脚本需要通过$u POST从Android应用程序中获取“name”,然后在选择查询中使用该字符串并将其传递回Android应用程序 除了只能将字符串传递给PHP脚本之外,我已经完成了所有操作,但是我不能在SELECT查询中使用它。这是我的密码: PHP代码: <?php include "../dbc.php"; //include "get_id.php"; $value = $_POST["value"]; $respo
<?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();
}
}
}
class GetUserSettings扩展了异步任务{
字符串json\u get\u settings\u url;
@凌驾
受保护的void onPreExecute(){
json_获取_设置_url=”http://url.com/json.php";
}
@凌驾
受保护的void onProgressUpdate(void…值){
super.onProgressUpdate(值);
}
@凌驾
受保护的字符串背景(无效…无效){
URL=null;
试一试{
url=新url(json\u获取\u设置\u url);
}捕获(格式错误){
e、 printStackTrace();
}
试一试{
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
InputStream InputStream=httpURLConnection.getInputStream();
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(inputStream));
StringBuilder StringBuilder=新的StringBuilder();
而((JSON_STRING=bufferedReader.readLine())!=null){
追加(JSON_字符串+“\n”);
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
返回stringBuilder.toString().trim();
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串JSON_字符串){
SharedReferences设置=GetSharedReferences(首选名称,模式\私有);
String value=settings.getString(“用户名”,“用户名”);
//这就是它开始从URL解析JSON的地方
试一试{
JSONObject jsonResponse=新的JSONObject(JSON_字符串);
JSONArray jsonMainNode=jsonResponse.optJSONArray(“服务器响应”);
for(int i=0;i
从代码中,您可以看到,我试图将字符串“value”传递给我的PHP代码,然后将“value”分配给$value,然后在SELECT查询中使用$value,但我如何才能传递字符串“value”,然后执行查询,以便使用JSON解析数据?您可以使用ContentValues()要将字符串传递给php amd,请返回select查询
ContentValues=新的内容值();
value.put(“字符串”,变量)
您可以将url作为
在php方面
$var=$\u GET['variable']:
现在,您可以在select查询中传递这个$var
希望这有帮助。您的Android代码没有向php脚本发布任何内容。因此$\u POST数组是空的。开始调整php代码,以检查是否输入了所需的参数/数据。如果没有,则将相关信息回显到Android客户端。