使用volley从mysql检索数据(多行)


Using volley to retrieve data(multiple rows) from mysql

我正在创建一个测验应用程序,我必须检索所有的问题显示。这是我的PHP和Java代码。我将把数据存储在另一个数组中。我无法从我的SQL表中获取任何数据。

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

代码问题:

  • 您混合了mysql_*mysqli_*扩展
  • 你的代码容易受到SQL注入,使用准备语句
  • 你可以更简单的取回
  • 尽量避免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);