为什么这个数据库查询返回null而不是JSONObject?

我有一个PHP类,它从数据库中检索数据,并将其编码为JSONArray,以便稍后在Android应用程序中使用它。由于某种原因返回null我不知道它有什么问题。为什么这个数据库查询返回null而不是JSONObject?

以下是文件:

<?php 

$response = array();

// include db connect class

require_once __DIR__ . '/db_connect.php';

// connecting to db

$db = new DB_CONNECT();

$dbh = $db->connect(); // here you get the connection

if(isset($_GET['TAG_ID'])){

$id = $_GET['TAG_ID'];

$query = "SELECT *FROM lost_pets WHERE id = '$id'";

$result = $dbh->prepare($query);

$result->execute();

if ($result->fetchAll() > 0) {

foreach($dbh->query($query) as $row){

$pet["name"] = $row['name'];

$pet["breed"] = $row['breed'];

$pet["type"] = $row['type'];

$pet["description"] = $row['description'];

$pet["pictures"] = $row['pictures'];

$pet["location"] = $row['location'];

$pet["locality"] = $row['locality'];

$pet["userid"] = $row['userid'];

echo json_encode($result->fetchAll(PDO::FETCH_ASSOC));

}

}

}

?>

Android的一面:

else if(method.equals("GET")){ 

// request method is GET

if (sbParams.length() != 0) {

url += "?" + sbParams.toString();

}

try {

urlObj = new URL(url);

conn = (HttpURLConnection) urlObj.openConnection();

conn.setDoOutput(false);

conn.setRequestMethod("GET");

conn.setRequestProperty("Accept-Charset", charset);

conn.setConnectTimeout(15000);

conn.connect();

is = conn.getInputStream();

} catch (IOException e) {

e.printStackTrace();

}

try {

BufferedReader reader = new BufferedReader(new InputStreamReader(

is, "iso-8859-1"));

StringBuilder sb = new StringBuilder();

String line;

while ((line = reader.readLine()) != null) {

sb.append(line);

}

is.close();

json = sb.toString();

elements = new JSONArray(json);

} catch (Exception e) {

Log.e("Buffer Error", "Error converting result " + e.toString());

}

return elements;

}

的ID是由应用程序给定的,我已经检查过是可能values.I有PHP缺乏了解,所以可能不是一个难以解决的问题,无论如何,我希望你能帮助我,谢谢!

回答:

我不知道这里真的发生了什么,但是删除了if子句解决了问题。感谢您的所有意见

以上是 为什么这个数据库查询返回null而不是JSONObject? 的全部内容, 来源链接: utcz.com/qa/257908.html

回到顶部