PHP不显示任何内容

时间:2014-01-12 14:03:48

标签: php android json

大家好我是PHP的新手,我正在尝试在我的管理表中显示所有信息。当我测试我的PHP文件它给我一个空白页面,这是我的PHP代码。没有错误,但我得到一个空白页面,当我执行它时没有返回任何内容。

 <?php

require('connect.inc.php');
require('admin.config.inc.php');
require('core.inc.php');

if (!empty($_POST)) {

    //initial query
    $query = "SELECT * FROM admin where username = :user";

    $query_params = array(':user' => $_POST['username']);

    //execute query
    try {
        $stmt = $db -> prepare($query);
        $result = $stmt -> execute($query_params);
    } catch (PDOException $ex) {
        $response["success"] = 0;
        $response["message"] = "Database Error!";
        die(json_encode($response));
    }

    // Finally, we can retrieve all of the found rows into an array using fetchAll 
    $rows = $stmt -> fetchAll();

    if ($rows) {
        $response["success"] = 1;
        $response["message"] = "Post Available!";
        $response["users"] = array();

        foreach($rows as $row) {
            $user = array();
            $user["username"] = $row["username"];
            $user["designation"] = $row["designation"];
            $user["middlename"] = $row["middle_initial"];
            $user["firstname"] = $row["first_name"];
            $user["lastname"] = $row["last_name"];

            //update our repsonse JSON data
            array_push($response["users"], $user);
        }

        // echoing JSON response
        echo json_encode($response);

    } else {
        $response["success"] = 0;
        $response["message"] = "No user available!";
        die(json_encode($response));
    }

} else {}


       ?>

3 个答案:

答案 0 :(得分:1)

将它放在脚本的头部,看看你得到了什么回应。然后其他人可能会根据你得到的错误帮助你。

ini_set('display_errors',1);
error_reporting(E_ALL);

我看到你也开始:

if (!empty($_POST)) {

}

$ _ POST IS通过HTTP POST方法传递给当前脚本的关联变量数组。

$ _ POST定义在哪里。这应该是$ _POST ['user']

之类的东西

答案 1 :(得分:0)

你在循环中声明$user = array();,它必须在循环之外

$user = array();

foreach($rows as $row) {           
            $user["username"] = $row["username"];
            $user["designation"] = $row["designation"];
            $user["middlename"] = $row["middle_initial"];
            $user["firstname"] = $row["first_name"];
            $user["lastname"] = $row["last_name"];

答案 2 :(得分:0)

首先:

if (!empty($_POST)) {

默认为空,所以你直接转到那个空的else语句!