【发布时间】:2014-12-29 12:14:53
【问题描述】:
我用 Mysql 数据库开发了一个安卓应用程序。我希望我的 sql 查询结果显示在表中的 JSON 变量中。如果我使用 TableA “country” ---> “id_country” 或 “name_en_country” 的单列,它可以工作,但如果我想显示更多列 ---> “id_country” AND “name_en_country” AND 更多...... . 结果 Requet php 给我发了一个空白页。你能帮我吗,谢谢!
<?php
// Create Database connection
$mysqli = new mysqli("localhost", "root", "", "whenmeeat");
if (!$mysqli) {
printf("Échec de la connexion : %s\n", mysqli_connect_error());
}
// Replace * in the query with the column names.
$result = $mysqli->query("select id_country, name_en_country, name_fr_country from country", MYSQLI_USE_RESULT);
// Create an array
$json_response["country"] = array();
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$row_array['id_country'] = $row['id_country'];
$row_array['name_en_country'] = $row['name_en_country'];
// push the values in the array
array_push($json_response["country"],$row_array);
}
echo json_encode($json_response["country"]);
// Close the database connection
$mysqli->close();
?>
【问题讨论】:
-
空白页通常意味着你没有配置 PHP 来显示错误信息。你需要先解决这个问题,而不是一直猜测。
-
如果我有 php 错误,我会出现一个错误页面。我已经尝试过了。
-
那么你可能只是得到
NULL。您可以使用var_dump(json_encode(...))进行验证,并且(如果是这种情况)您可以使用json_last_error() 看到错误。我的印象是您没有使用正确的 UTF-8 提供 JSON 函数。