0
我有一個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缺乏瞭解,所以可能不是一個難以解決的問題,無論如何,我希望你能幫助我,謝謝!
什麼是完全返回NULL。在'connect()'? –
@niCkcAMel我認爲$結果返回null –
'SELECT * FROM' - >'SELECT * FROM' - 不完全確定這是導致問題,但..我認爲這是 – icecub