json不输出
我在 php 中运行此代码
while ($row = mysql_fetch_array($result))
{
$arr = array("joke" => $row['joke'], "date" => $row['date'], "rating" => $row['rating']);
echo json_encode($arr);
}
,但没有输出。我正在运行 php 5.3.6
I'm running this code in php
while ($row = mysql_fetch_array($result))
{
$arr = array("joke" => $row['joke'], "date" => $row['date'], "rating" => $row['rating']);
echo json_encode($arr);
}
but there's no output. I am running php 5.3.6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的查询很可能失败。由于语法错误或只是不匹配任何内容。重做您的代码,使其看起来像这样:
or die
部分将处理查询错误并导致错误的情况。将 $data 设置为空数组最初可以确保您从 json_encode 中获得一些内容,即使它只是一个空的 javascript 数组。然后 while 循环吸收查询结果并将其填充到 $data 数组中。Most likely your query's failing. Either due to syntax errors or just not matching anything. Redo your code so it looks something like this:
The
or die
portion will handle the case when the query's bad and causes an error. setting $data to an empty array initially ensures that you'll get SOMETHING out of json_encode, even if it's just an empty javascript array. And then the while loop sucks the query results and stuffs into the $data array.nvm我明白了。执行此操作的方法是使用 sql2json
nvm I figured it out. the way to do this is to use sql2json