在android中使用php从mysql数据库获取数据

时间:2022-09-25 19:05:16

I am trying to access an individual player in the database, I am getting the response {"success":0,"message":"Required field(s) is missing"} but this is wrong and should be outputting a single player field.

我正在尝试访问数据库中的单个玩家,我收到响应{“成功”:0,“消息”:“必填字段丢失”}但这是错误的,应输出单个玩家字段。

I think the problem is with the php code but I am not certain. Any help is greatly appreciated, thanks.

我认为问题在于PHP代码,但我不确定。非常感谢任何帮助,谢谢。

php code -

PHP代码 -

<?php

require('db_connection.php');

// check for post data
if (isset($_GET["playerid"])) {
$playerid = $_GET['playerid'];

// get a player from week1 table
$result = mysql_query("SELECT *FROM week1 WHERE playerid = $playerid");

if (!empty($result)) {
    // check for empty result
    if (mysql_num_rows($result) > 0) {

        $result = mysql_fetch_array($result);

        $player = array();
        $player["playerid"] = $result["playerid"];
        $player["score"] = $result["score"];
        $player["lastholeplayed"] = $result["lastholeplayed"];
        $player["overall"] = $result["overall"];

        // success
        $response["success"] = 1;

        // user node
        $response["player"] = array();

        array_push($response["player"], $player);

        // echoing JSON response
        echo json_encode($response);
    } else {
        // no player found
        $response["success"] = 0;
        $response["message"] = "No player found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // no player found
    $response["success"] = 0;
    $response["message"] = "No player 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);
}
?>

related java class -

相关的java类 -

public class InputScores extends Activity {


// Progress Dialog
    private ProgressDialog pDialog;

    // Creating JSON Parser object


    ArrayList<HashMap<String, String>> holesList;

    // url to get all products list
    private static String url_update_players = "http://192.168.2.4/realdeal/getplayer.php";

    // JSON Node names


    // products JSONArray
    JSONArray courseone = null;

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (pDialog != null) {
            pDialog.cancel();
        }
    }

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.updatescores);

    Intent i = getIntent();
    i.getStringExtra("playerid");

    //String playerid = i.getStringExtra("playerid");
    //System.out.println(playerid);
    //TextView myTextView = (TextView)findViewById(R.id.);
    //myTextView.setText(playerid);
    //String playerid = "999";
    new loadplayerdetails().execute();

}
/**
 * Background Async Task to Load all holes by making HTTP Request
 * */
class loadplayerdetails extends AsyncTask<String, String, String> {




    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(InputScores.this);
        pDialog.setMessage("Loading holes. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting All holes from url
     * */
    protected String doInBackground(String... params) {

        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url_update_players);

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                    2);
            String playerid ="5";
            nameValuePairs.add(new BasicNameValuePair("playerid", playerid));



            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            ResponseHandler<String> responseHandler = new BasicResponseHandler();

            String response = httpclient.execute(httppost,
                    responseHandler);
            // you will get json string here

            // check here your getting json string in logcat.
            Log.d("response", response);



        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all players
        if (pDialog.isShowing()) {
            pDialog.dismiss();
        }


    }

}

}

1 个解决方案

#1


1  

You're posting your data not using "get".

您使用“get”发布数据。

You use $_GET when you make your call with everything in the URL:

当您使用URL中的所有内容进行通话时,您可以使用$ _GET:

http://blahblah.com/somescript.php?playerid=1234

In your code, you used $_GET to get it. When you're posting, as you did use $_POST:

在您的代码中,您使用$ _GET来获取它。当您发布时,正如您使用$ _POST一样:

if (isset($_POST["playerid"])) {
    $playerid = $_POST['playerid'];

#1


1  

You're posting your data not using "get".

您使用“get”发布数据。

You use $_GET when you make your call with everything in the URL:

当您使用URL中的所有内容进行通话时,您可以使用$ _GET:

http://blahblah.com/somescript.php?playerid=1234

In your code, you used $_GET to get it. When you're posting, as you did use $_POST:

在您的代码中,您使用$ _GET来获取它。当您发布时,正如您使用$ _POST一样:

if (isset($_POST["playerid"])) {
    $playerid = $_POST['playerid'];