所以我有这个代码用于从android中使用php从mysql中检索数据。 它读取"序列号"我输入然后它读取和检索数据作为具有该特定SN的数组。
一切都有效,直到我有一个序列号,它是数字和字母字符的组合(AJH871)。 它不会检索特定数据。显然,除了数字(289173等)以外的任何东西都不起作用,尽管没有错误。
知道如何解决这个问题吗?
以下是代码的一部分: (这里是检索代码并显示为gettext的地方)
class GetProductDetails extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(EditProductActivity.this);
pDialog.setMessage("Loading product details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting product details in background thread
* */
protected String doInBackground(String... params) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("SN",SN));
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(
url_product_details, "GET", params);
// check your log for json response
Log.d("Single Product Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray productObj = json
.getJSONArray(TAG_PRODUCT); // JSON Array
// get first product object from JSON Array
JSONObject product = productObj.getJSONObject(0);
// product with this pid found
// Edit Text
txtID = (EditText) findViewById(R.id.inputID);
txtSerial = (EditText) findViewById(R.id.inputSerial);
txtJenis = (EditText) findViewById(R.id.inputJenis);
txtMerk = (EditText) findViewById(R.id.inputMerk);
//txtSpec = (EditText) findViewById(R.id.inputSpec);
txtUser = (EditText) findViewById(R.id.inputUser);
txtDept = (EditText) findViewById(R.id.inputDept);
txtCond = (EditText) findViewById(R.id.inputCond);
// display product data in EditText
txtID.setText(product.getString(TAG_PID));
txtSerial.setText(product.getString(TAG_SERIAL));
txtJenis.setText(product.getString(TAG_JENIS));
txtMerk.setText(product.getString(TAG_MERK));
//txtSpec.setText(product.getString(TAG_SPEC));
txtUser.setText(product.getString(TAG_USER));
txtDept.setText(product.getString(TAG_DEPT));
txtCond.setText(product.getString(TAG_COND));
check = 1;
}else{
// product with pid not found
check = 2;
}
} catch (JSONException e) {
e.printStackTrace();
}
if(check == 1){
Toast.makeText(getApplicationContext(), "Data ditemukan",Toast.LENGTH_SHORT).show();
}
else if(check==2){
Toast.makeText(getApplicationContext(), "ID tidak ditemukan",Toast.LENGTH_SHORT).show();
}
}
});
return null;
}
//
这里是jsonparser
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
//
这里是php
if (isset($_GET["SN"])) {
$SN = $_GET['SN'];
// get a product from products table
$result = mysql_query("SELECT *FROM products WHERE SN = $SN");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$product = array();
$product["SN"] = $result["SN"];
$product["label"] = $result["label"];
$product["jenis"] = $result["jenis"];
$product["merk"] = $result["merk"];
$product["user"] = $result["user"];
$product["dept"] = $result["dept"];
$product["cond"] = $result["cond"];
// success
$response["success"] = 1;
// user node
$response["products"] = array();
array_push($response["products"], $product);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
//
对不起,如果它有点乱。
答案 0 :(得分:1)
试试这个:
SQL查询的正确引号
$result = mysql_query("SELECT *FROM products WHERE SN = '$SN'");
由此:
SELECT *FROM products WHERE SN = ABC123;
对此:
SELECT *FROM products WHERE SN = 'ABC123';
答案 1 :(得分:0)
我猜您SN COLUMN
不是int
有点datatype
。
如果它是varchar
或其他string friendly
datatypes
,您必须在=
(等号)之后使用单引号。
类似于:='searchTermHere'
会这样做,对于您的情况='$SN'
应该这样做