我已经在php文件中编写了这个查询来从数据库中获取数据,它工作正常并获得所需的数据。 但是如何在json fromat中打印检索到的数据以使用Web服务
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE visible = 1 ";
$query .= "ORDER BY position ASC";
$result = mysqli_query($connection, $query);
$subject = mysqli_fetch_assoc($result);
print_r($subject);
必须要做什么代码才能帮助我。
答案 0 :(得分:0)
你试过json_encode吗?像:
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE visible = 1 ";
$query .= "ORDER BY position ASC";
$result = mysqli_query($connection, $query);
$subject = mysqli_fetch_assoc($result);
echo json_encode($subject);
有关详细信息,请查看:PHP Manual
答案 1 :(得分:0)
试试这个:
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE visible = 1 ";
$query .= "ORDER BY position ASC";
$result = mysqli_query($connection, $query);
$subject = mysqli_fetch_assoc($result);
header('Content-Type: application/json');
echo json_encode($subject);