响应不包含任何数据。 PHP脚本

时间:2016-04-10 05:20:44

标签: php

我正在尝试在我的项目上处理我的登录模块,但它总是返回

  

响应不包含任何数据。

我认为问题是我的 DB_Functions.php ,因为我可以浏览 index.php

DB_Functions.php

的代码
<?php

class DB_Functions {

    private $db;

    //put your code here
    // constructor
    function __construct() {
        require_once 'DB_Connect.php';
        // connecting to database
        $this->db = new DB_Connect();
        $this->db->connect();
    }

    // destructor
    function __destruct() {

    }

    public function getUserByEmailAndPasswordStudent($studentno, $password) {
        $result = mysql_query("SELECT * FROM students WHERE student_no = '$studentno'") or die(mysql_error());
        // check for result 
        $no_of_rows = mysql_num_rows($result);
        if ($no_of_rows > 0) {
            $result = mysql_fetch_array($result);
            $pass = $result['password'];
            // check for password equality
            if ($pass == $password) {
                // user authentication details are correct
                return $result;
            }
        } else {
            // user not found
            return false;
        }
    }
}

index.php

的代码
<?php

if (isset($_POST['tag']) && $_POST['tag'] != '' && $_POST['tag'] != '') {
    // get tag
    $tag = $_POST['tag'];

    // include db handler
    require_once 'include/DB_Functions.php';
    $db = new DB_Functions();

    // response Array
    $response = array("tag" => $tag, "error" => FALSE);

    // check for tag type
    if ($tag == 'studentlogin') {
        // Request type is check Login
        $studentno = $_POST['studentno'];
        $password = $_POST['password'];

        // check for user
        $user = $db->getUserByEmailAndPasswordStudent($studentno, $password);
        if ($user != false) {
            // user found
            $response["error"] = FALSE;
            $response["user"]["studentid"] = $user["studentid"];
            $response["user"]["fname"] = $user["fname"];
            $response["user"]["mname"] = $user["mname"];
            $response["user"]["lname"] = $user["lname"];
            $response["user"]["student_no"] = $user["studentno"];
            $response["user"]["grade_level"] = $user["gradelevel"];
            $response["user"]["sectionid"] = $user["sectionid"];
            echo json_encode($response);
        } else {
            // user not found
            // echo json with error = 1
            $response["error"] = TRUE;
            $response["error_msg"] = "Incorrect email or password!";
            echo json_encode($response);
        }
    }
} else {
    $response["error"] = TRUE;
    $response["error_msg"] = "Required parameter 'tag' is missing!";
    echo json_encode($response);
}
?>

我的代码有什么问题?

1 个答案:

答案 0 :(得分:1)

$tag != 'studentlogin'时条件没有if ($tag == 'studentlogin') { /* your code here*/ } else { echo json_encode($response); } ,因为在这种情况下你没有回应任何东西。你的代码应该是这样的:

error_reporting(E_ALL)

如果此解决方案无法帮助您在文件顶部添加{{1}}并将调试输出添加到您的问题中。