Android将$_GET字符串传递给php并转换为整数


Android pass $_GET string and pass to php convert as integer

是否可以将字符串变量从android发送到php,但将字符串转换为integer。因为当我尝试将字符串转换为int,然后将其传递到php文件时,会出现错误。这是代码。

package com.example.taxamsp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.example.taxamsp.extra.alertManager;
import com.example.taxamsp.extra.connectionDetector;
import com.example.taxamsp.extra.JSONParser;
public class subjectInfo extends ListActivity {
// Connection detector
connectionDetector cd;
// Alert dialog manager
alertManager alert = new alertManager();
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String, String>> examlist;
// tracks JSONArray
JSONArray exam = null;
// Subject id
String subject_id;
// subjects exam JSON url
private static final String URL_EXAM = "http://192.168.43.100/tx/subjectexams.php?course=";
// ALL JSON node names
private static final String TAG_ID = "id";
private static final String TAG_IDCOURSE = "course";
private static final String TAG_NAME = "name";
private static final String TAG_INTRO = "intro";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.subject_info_layout);
    cd = new connectionDetector(getApplicationContext());
    // Check if Internet present
    if (!cd.isConnectingToInternet()) {
        // Internet Connection is not present
        alert.showAlertDialog(subjectInfo.this, "Internet Connection Error",
                "Please connect to working Internet connection", false);
        // stop executing code by return
        return;
    }
    // Get subject id
    Intent i = getIntent();
    subject_id = i.getStringExtra(subject_id);
    // Hashmap for ListView
    examlist = new ArrayList<HashMap<String, String>>();
    // Exams in Background Thread
    new LoadExams().execute();
    // get listview
    ListView lv = getListView(); 
}
/**
 * Background Async Task to Load All Exam
 * */
class LoadExams extends AsyncTask<String, String, String> {
    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(subjectInfo.this);
        pDialog.setMessage("Loading exams ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }
    /**
     * getting Subjects JSON
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // post subject id as GET parameter
        //params.add(new BasicNameValuePair(subject_id, subject_id));
        // getting JSON string from URL
        String json = jsonParser.makeHttpRequest(URL_EXAM + subject_id, "GET",
                params);
        // Check your log cat for JSON reponse
        Log.d("Subjects JSON: ", "> " + json);
        try {               
            exam = new JSONArray(json);
            if (exam != null) {
                for (int i = 0; i < exam.length(); i++) {
                    JSONObject c = exam.getJSONObject(i);
                    // Storing each json item values in variable
                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_NAME);
                    String intro = c.getString(TAG_INTRO);
                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();
                    // adding each child node to HashMap key => value
                    map.put(subject_id, subject_id);
                    map.put(TAG_ID, id);
                    map.put(TAG_NAME, name);
                    map.put(TAG_INTRO, intro);
                    // adding HashList to ArrayList
                    examlist.add(map);
                }
            }else{
                Log.d("Subjects: ", "null");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(String file_url) {
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        subjectInfo.this, examlist,
                        R.layout.e_content, new String[] { "subject_id", TAG_ID, TAG_NAME,
                                TAG_INTRO }, new int[] {
                                R.id.subject_id, R.id.quiz_id, R.id.quiz_name, R.id.quiz_intro});
                // updating listview
                setListAdapter(adapter);
            }
        });
    }
}
}

这是我的php文件。

<?php
include("config.php"); 
$x = $_GET["subject_id"];
    $query = "SELECT id, course, name, intro FROM mdl_quiz WHERE course =".$x."";
$result = mysql_query($query);
$obj = array(); 
$i = 0;
while($row=mysql_fetch_assoc($result)){
    $obj[$i]['id'] = $row["id"];
    $obj[$i]['course'] = $row["course"]; 
    $obj[$i]['name'] = $row["name"];
    $obj[$i]['intro'] = $row["intro"];
    $i++; 
}
echo json_encode($obj);
mysql_close();  

?>

当我运行它时,它在传递$_GET值并在查询中使用时崩溃。我需要帮助。提前谢谢!

行:

$query = "SELECT id, course, name, intro FROM mdl_quiz WHERE course =".$x."";

结尾有一个额外的双引号。

尝试:

$query = "SELECT id, course, name, intro FROM mdl_quiz WHERE course =".$x.";

此外,我非常确信,即使$_GET['subject_id']中的值是数字,PHP也会将其视为字符串。