douwen1006 2017-04-14 18:16
浏览 25
已采纳

没有来自PHP的JSON响应

I have a JSON POST request being sent to index.php as part of a login application for a mobile device. 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. The $user below isn't doing anything as I'm just debugging.

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

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? I'm not sure how to call it.

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;

W/System.err: org.json.JSONException: End of input at character 0

[EDIT]

Alright so I have taken the following lines from DB_Connect.php and put them into the method in 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;

$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.

  • 写回答

1条回答 默认 最新

  • dsjpqpdm620596 2017-04-14 20:40
    关注

    I have taken the following lines from DB_Connect.php and put them into the method 'getUserByIdAndPassword' in 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;

    $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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退