我正在创建一个测验应用程序,我必须回顾所有要显示的问题。这是我的php和java code.i会将数据存储在另一个数组中。我无法从我的数据中获取任何数据sql table。
StringRequest stringRequest = new StringRequest(Request.Method.POST,insertUrl,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
jsonArray=new JSONArray(response);
JSONObject jsonObject=jsonArray.getJSONObject(0);
for(int k=0;k<jsonArray.length();k++)
{
question[k]=jsonObject.getString("question");
opta[k]=jsonObject.getString("optionA");
optb[k]=jsonObject.getString("optionB");
optc[k]=jsonObject.getString("optionC");
optd[k]=jsonObject.getString("optionD");
ans[k]=jsonObject.getString("Answers");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Quiz.this,error.toString(),Toast.LENGTH_LONG ).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<String,String>();
params.put(ITEM_COURSE,data);
return super.getParams();
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
这是我的php代码
<?php
require 'initquiz.php';
global $connect;
$response = array();
$course=$_POST["course"];
$query = "SELECT * FROM questions WHERE course='$course'";
$result= mysqli_query($connect,$query) or die(mysqli_error($connect));
$response= array();
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
$question = $row[0];
$optionA = $row[2];
$optionB= $row[3];
$optionC = $row[4];
$optionD= $row[5];
$Answers= $row[6];
array_push($response,array("question"=>$question,"optionA"=>$optionA,"optionB"=>$optionB,"optionC"=>$optionC,"optionD"=>$optionD,"Answers"=>$Answers));
}
}
echo json_encode($response);
?>
答案 0 :(得分:0)
您的代码问题:
mysql_*
和mysqli_*
分机SELECT *
,而是选择特定字段尝试这种方法:
$response = [];
if(isset( $_POST["course"])){
$mysqli = new mysqli("host", "user", "password", "db");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT * FROM questions WHERE course = ?";
if ($stmt = $mysqli->prepare($query)) {
$stmt->bind_param("s", $_POST["course"]);
$stmt->execute();
while ($row = $stmt->fetch_assoc()) {
$response['data'][] = $row;
}
$response['success'] = true;
}
$mysqli->close();
}else{
$response['data'] = 'No Course Sent';
$response['success'] = false;
}
echo json_encode($response);