将变量发送到PHP并从MySQL中检索与变量匹配的特定行

时间:2018-05-29 05:18:19

标签: java php android mysql

现在我正在制作预订申请。因此,我试图将变量发送到PHP并提取与我发送的变量匹配的行。具体来说,将userBranch从Android工作室发送到PHP,并使用userBranch提取courseBranch具有相同值的行。我在哪里设置条件,但它无法检索数据。 这是我的PHP代码(CourseList.php)

<?php
header("Content-Type: text/html; charset=UTF-8");

$con = mysqli_connect($host, $username, $pwd, $db) or die('Unable to connect');

$courseBranch = $_GET["userBranch"]

if(mysqli_connect_error($con))
{
    echo "Failed to Connect to Database".mysqli_connect_error();
}

$sql = "SELECT courseID, courseTeacher, courseDay, courseTime, courseBranch  FROM COURSE WHERE courseBranch = '$courseBranch'";

$result = mysqli_query($con,$sql);
if($result)
{
    while($row = mysqli_fetch_array($result))
    {
        $flag[] = $row;
    }

    print(json_encode($flag));

}

mysqli_close($con);

?>

这是我解析数据的Android工作室代码

private int parseData(){
    try {
        JSONArray ja = new JSONArray(jsonData);
        JSONObject jo = null;

        courseTeacher.clear();
        courseDay.clear();
        courseTime.clear();
        courseTeacherParse.clear();


        for(int i = 0; i <ja.length();i++)
        {
            jo = ja.getJSONObject(i);
            int id = jo.getInt("courseID");
            String name = jo.getString("courseTeacher");
            String day = jo.getString("courseDay");
            String time = jo.getString("courseTime");
            String branch = jo.getString("courseBranch");

            course = new Course();


            course.setCourseTeacher(name);
            course.setCourseDay(day);
            course.setCourseTime(time);


            courseTeacher.add(name);
            courseDay.add(day);
            courseTime.add(time);
            courseBranch.add(branch);


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

这是urladdress

String urlAddress = "";
    try{
        urlAddress = "http://show981111.cafe24.com/CourseList.php?userBranch=" + URLEncoder.encode(userBranch, "UTF-8");
    }catch (Exception e)
    {
        e.printStackTrace();
    }

0 个答案:

没有答案