gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:31:52 25 4
gpt4 key购买 nike

我正在尝试访问数据库中的单个玩家,我收到响应 {"success":0,"message":"Required field(s) is missing"} 但这是错误的,应该输出单人游戏领域。

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

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);
}
?>

相关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();
}


}

}

最佳答案

您没有使用“get”发布您的数据。

当您使用 URL 中的所有内容进行调用时,您使用 $_GET:

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

在您的代码中,您使用了 $_GET 来获取它。当你发帖时,就像你使用 $_POST 一样:

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

关于php - 在android中使用php从mysql数据库获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16158386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com