PHP未返回预期结果

时间:2019-01-15 15:54:26

标签: java php mysql

我得到一个非常奇怪的结果!我从Java类发布了id,其中的id将在php脚本中用于检索特定数据。 value应该为1,但始终显示2

<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
    //Getting values 
    $id = $_POST['id'];

    //Creating sql query
    $sql = "SELECT xuenian FROM student WHERE sid='$id'";

    //importing dbConnect.php script 
    require_once('db_config.php');

    //executing query
    $result = mysqli_query($con,$sql);

    $value = mysqli_fetch_object($result);
    $value->xuenian;

    if($value === "1"){
        echo "1";
    }else{
         echo "2";
    }
    mysqli_close($con);
}

我尝试过==,结果仍然相同。

Java类

 public void loadResults(final String id, final int xuenian) {

        StringRequest stringRequest = new StringRequest(Request.Method.POST, AppConfig.URL_CHECKID,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(getApplication(),response+"from php",Toast.LENGTH_LONG).show();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getApplication(), error + "", Toast.LENGTH_LONG).show();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                //Adding parameters to request
                params.put(AppConfig.KEY_USERID, id);
                //returning parameter
                return params;
            }
        };

        //Adding the string request to the queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

1 个答案:

答案 0 :(得分:4)

您要将$value设置为此处的对象:

$value = mysqli_fetch_object($result);

然后此行不执行任何操作:

$value->xuenian;

在下一行,$value仍然是一个对象,但是您正在将它与一个字符串进行比较,该字符串将始终为false:

if($value === "1")
{
    echo "1";
}else{
    echo "2";
}

您可能想要这样:

if($value->xuenian === "1")