所以我有这段代码,用于使用来自 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);
}
//
如果有点乱,请见谅。
最佳答案
试试这个:
SQL 查询中的正确引用
$result = mysql_query("SELECT *FROM products WHERE SN = '$SN'");
来自这里:
SELECT *FROM products WHERE SN = ABC123;
对此:
SELECT *FROM products WHERE SN = 'ABC123';
关于php 只检索数字,即使是字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24357714/