我是 PHP 新手并试图制作一个非常简单的 api(我认为也是学习的好方法),为什么这不起作用?加载此页面时,输出为:{"queryString":"SELECT id, Name FROM Food WHERE userID=1 ORDER BY Name"}而不是我想要的 JSON 字符串
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
$response = $dbh->query("SELECT id, Name FROM Food WHERE userID=1 ORDER BY Name");
$responseJSON = json_encode($response);
header('Content-type: application/json');
echo "$responseJSON";
$dbh = null;
查询没有任何问题,因为我在尝试此操作时得到了我想要的:
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
$response = $dbh->query("SELECT id, Name FROM Food WHERE userID=1 ORDER BY Name");
foreach ($response as $row)
{
print $row['Name'] . ' ';
}
$dbh = null;
输出: 培根芝士片