简体   繁体   English

没有来自PHP的JSON响应

[英]No JSON response from PHP

I have a JSON POST request being sent to index.php as part of a login application for a mobile device. 我有一个JSON POST请求被发送到index.php作为移动设备的登录应用程序的一部分。 I'm using an old script so I believe the problem is deprecated syntax with the PHP, as my response JSON is coming back empty. 我正在使用一个旧脚本,因此我认为问题是PHP的语法已被弃用,因为我的响应JSON回来了。 The $user below isn't doing anything as I'm just debugging. 下面的$ user没有做任何事情,因为我只是在调试。

index.php 的index.php

if (isset($_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 == 'login') {

    $id = $_POST['id'];
    $password = $_POST['password'];
    $user_type = $POST['user'];


// test data
    $response = array('tag' => $tag, 'error' => FALSE, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

    // check for user
    $user = $db->getUserByIdAndPassword($user_type, $id, $password);
        echo json_encode($response);
    }
} else {
    $response["error"] = TRUE;
    $response["error_msg"] = "Required parameter 'tag' is missing!";
    echo json_encode($response);
}

The query is run in this class, which is where I suspect I'm going wrong. 查询在这个类中运行,这是我怀疑我出错的地方。

DB_Functions.php DB_Functions.php

class DB_Functions {

private $db;

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

// destructor
function __destruct() {

}


/**
 * Get user by id and password
 */
public function getUserByIdAndPassword($user_type, $id, $password) {
    $t = 'T';
    if ($user_type !== $t) {
        $result = mysqli_query("SELECT * FROM smiths WHERE id = '$id'") or die(mysqli_error());
    } else {
        $result = mysqli_query("SELECT * FROM traders WHERE id = '$id'") or die(mysqli_error());
    }
    // check for result 
    $no_of_rows = mysqli_num_rows($result);
    if ($no_of_rows > 0) {
        $result = mysqli_fetch_array($result);
        $retrieved_password = $result['password'];
        // check for password equality
        if ($retrieved_password == $password) {
            return $result;
        }
    } else {
        // user not found
        return false;
    }
}

} }

And finally this class manages the msql connection, I think I need the $con from here for the previous mysqli_query? 最后这个类管理msql连接,我想我需要来自这里的$ con以用于之前的mysqli_query? I'm not sure how to call it. 我不知道怎么称呼它。

DB_Connect.php DB_Connect.php

class DB_Connect {

// constructor
function __construct() {

}

// destructor
function __destruct() {
    // $this->close();
}

// Connecting to database
public function connect() {
    require_once 'include/Config.php';
    // connecting to mysql
    $con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
    // Check connection
    if (!$con)
      {
        die("Connection error: " . mysqli_connect_error());
      }
    // selecting database
    mysqli_select_db($con, DB_DATABASE) or die(mysqli_connect_error());

    // return database handler
    return $con;
}

// Closing database connection
public function close() {
    mysqli_close();
}

} }

Could anyone help set me in the right direction? 任何人都可以帮助我朝着正确的方向前进吗? The JSON response is coming back with the error; JSON响应随着错误而回复;

W/System.err: org.json.JSONException: End of input at character 0 W / System.err:org.json.JSONException:字符0处的输入结束

[EDIT] [编辑]

Alright so I have taken the following lines from DB_Connect.php and put them into the method in DB_Functions.php. 好吧,我从DB_Connect.php中取出以下几行,并将它们放入DB_Functions.php中的方法中。

$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysqli_select_db($con, DB_DATABASE) or die(mysqli_connect_error());

This then allows me to fix the syntax of msqli_query as so; 然后,这允许我修复msqli_query的语法;

$result = mysqli_query($con, "SELECT * FROM smiths WHERE id = '$id'") or die(mysqli_error());

This has fixed my issue, however hacky/messy it may seem. 这解决了我的问题,但看起来很乱/乱。

I have taken the following lines from DB_Connect.php and put them into the method 'getUserByIdAndPassword' in DB_Functions.php. 我从DB_Connect.php中取了以下几行,并将它们放入DB_Functions.php中的方法'getUserByIdAndPassword'中。

$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysqli_select_db($con, DB_DATABASE) or die(mysqli_connect_error());

This then allows me to fix the syntax of msqli_query as so; 然后,这允许我修复msqli_query的语法;

$result = mysqli_query($con, "SELECT * FROM smiths WHERE id = '$id'") or die(mysqli_error())

And this kids is why you don't use deprecated code like some Frankenstein madman. 这个孩子就是为什么你不像一些弗兰肯斯坦疯子那样使用弃用的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM