【发布时间】:2015-05-30 08:38:42
【问题描述】:
我想通过 json 将带有 PHPH 文件的数据库记录发送到我使用 IntelXDK 制作的应用程序。因为我不能在英特尔 XDK 中使用 PHP 代码,所以我需要使用 JSON。我想在屏幕上的“报价”表中显示两条记录“报价”和“作者”。有人帮我编写了这段代码,但它只返回 [null,null] 而不是我需要的两条记录。我尝试调试,但我是 PHP 新手,所以我无法让它工作。任何可以帮助或看到此代码中的错误?谢谢!
PS:是的,我现在已经有其他人就这个主题提出了多个问题。我已经阅读了所有内容,但没有一个能解决我的问题..
<?php
if(isset($_GET["get_rows"]))
{
//checks the format client wants
if($_GET["get_rows"] == "json")
{
$link = mysqli_connect("localhost", "xxxxx", "xxxxx", "xxxx");
/* check connection */
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
header("HTTP/1.0 500 Internal Server Error");
exit();
}
$query = "SELECT quote, author FROM quotes WHERE id = " . date('d');
$jsonData = array();
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
$row = $result->fetch_assoc($result);
// Create a new array and assign the column values to it
// You can either turn an associative array or basic array
$ret= array();
$ret[] = $row['quote'];
$ret[] = $row['author'];
//encode to JSON format
echo json_encode($ret);
}
else {
echo json_encode($ret);
}
/* close connection */
mysqli_close($link);
}
else
{
header("HTTP/1.0 404 Not Found");
}
}
else
{
header("HTTP/1.0 404 Not Found");
}
?>
【问题讨论】:
标签: php mysql sql json database