PHP进行查询并返回json null?!为什么

时间:2016-07-13 23:48:07

标签: java php android mysql json

这是我的php:

<?php
// branch on the basis of 'calculate' value 
include('../php/config.php');
$email = $_POST['email'];
$password = $_POST['pass'];
$result = mysqli_query($db,"select * from Utenti where Email LIKE '$email' AND Password = '$password' ");
if (mysqli_num_rows($result)>0) {
    while($row = $result ->fetch_assoc())
    {
        $GetIdU = $row['ID'];
    }
    switch ($_POST['action']) {
        case "GetAllContact":
            $AddC =  $db->query("select * from Rubrica where ID_Utente = '$GetIdU'");
            if ($rAddC->num_rows > 0) {
                $srows = array();
                while($row = $AddC ->fetch_assoc())
                {
                    $rows=$row;
                }
                echo json_encode($rows);
            }
            break;
        case 'LoadAllContact':
            echo $_POST['number_1'] . " - " . $_POST['number_2'] . " = " . ($_POST['number_1']-$_POST['number_2']);
            break;
        case 'GetAllContact':
            echo $_POST['number_1'] . " x " . $_POST['number_2'] . " = " . ($_POST['number_1']*$_POST['number_2']);
            break;
        default:
            echo "Azione sconosciuta";
    }
}
?>

这是我的java代码:

 // HTTP POST request
    private static void sendPost(String post) throws Exception {

        String url = "myurl";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        //add reuqest header
        con.setRequestMethod("POST");
        con.setRequestProperty("User-Agent", USER_AGENT);
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
        //"sn=C02G8416DRJM&cn=&locale=&caller=&num=12345"
        String urlParameters = post;

        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + urlParameters);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        String jsonText = response.toString();
        JSONObject json = new JSONObject(jsonText);
        System.out.print(json.toString());
        //print result
        Log.d("Data: ",response.toString());

    }

我也有一个html页面,我检查了连接到数据库。 问题似乎并不存在,也通过从php的简单打印,它工作,但查询似乎没有返回任何结果。 我检查了查询,似乎没有错误,数据在数据库内部,我在哪里做错了? 谢谢你的时间,你是一个很棒的社区!

0 个答案:

没有答案