使用齐射从mysql中检索数据(多行)

问题描述:

我正在创建一个测验应用程序,我必须回顾所有问题以显示。这是我的php和java code.i'll将数据存储在另一个数组中。我无法从我的sql表中获取任何数据。使用齐射从mysql中检索数据(多行)

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