Java 添加到http请求的NameValuePair
好的,我需要根据一些参数从在线数据库中检索特定字段。Java 添加到http请求的NameValuePair,java,android,Java,Android,好的,我需要根据一些参数从在线数据库中检索特定字段。 参数必须从Android应用程序传递到PHP脚本(我有一个参数可以使用,但多个参数都无法使用) PHP脚本 <?php error_reporting (E_ALL ^ E_NOTICE); ?> <?php $username = "root"; $password = ""; $host = "localhost"; $dbname = "db.projectblue"; $chapter = mysql_real_
参数必须从Android应用程序传递到PHP脚本(我有一个参数可以使用,但多个参数都无法使用) PHP脚本
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php
$username = "root";
$password = "";
$host = "localhost";
$dbname = "db.projectblue";
$chapter = mysql_real_escape_string($_POST['column']);
$id = mysql_real_escape_string($_POST['id']);
mysql_connect($host, $username, $password);
mysql_select_db($dbname);
$q=mysql_query("SELECT '$column' FROM tblphysics WHERE id='$id'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
?>
当我使用php脚本中的两个变量调用此公共方法时,输出(来自另一个方法的日志)如下所示:
我的问题是:为什么php脚本不能处理多个变量
我的NameValuePair设置是否正确
是否可以创建具有动态列名和/或表名的SQL查询 编辑:
修复是如此简单,我甚至没有看到它。我在查询中有$column而不是$chapter,查询中的变量必须是“$chapter”。提示:在查询之前,为什么不尝试回显$coulmn并查看$column中存储的值 你可以参考这个例子,消除一些疑虑
无论是客户端还是服务器端,都不需要卸载变量,整个mysql模块在PHP中都是不推荐使用的。我会使用PDO或mysqli来完成你想要的,这个网站上有很多这样做的例子。
private JSONObject getJSONObject(int id, String column) {
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("column", column));
nameValuePair.add(new BasicNameValuePair("id", Integer.toString(id)));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(PARSER_URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e(LOG_TAG_JSON, "Error in http connection " + e.toString());
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
JSONArray jArray = new JSONArray(result);
JSONObject json_data = jArray.getJSONObject(0);
return json_data;
} catch (JSONException e) {
Log.e(LOG_TAG_JSON, "Error parsing data " + e.toString());
}
return null;
}
public String getChapter(int id) throws JSONException {
JSONObject object = getJSONObject(id, "chapter");
String iChapter = object.optString("chapter");
return iChapter;
}
Version (this is a column name in db) : 0 (should be 5)
Chapter (this is a column name in db) : chapter (should be newton)