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 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛