MySQL 结果数组...
我在这里做错了什么?
我正在尝试返回一个 json 对象,但似乎无法通过该数组...我已经构建了数百个常规数组并将它们作为 json 对象返回,但我很难理解这个数组。
$rows = array();
$post_array = array();
$i = 0;
$result = mysql_query(" SELECT * FROM forum_posts WHERE permalink = '$permalink' AND LOWER(raw_text) LIKE '%$str%' " );
while($row = mysql_fetch_assoc($result))
{
$post_array[$i] = $rows[ "id" => htmlentities($row["id"]),
"post_content" => htmlentities($row["content"]),
"author" => $row["author"],
"last_updated" => $row["last_updated"],
"author_id" => $row["author_id"],
"editing_author" => $row["editing_author"],
"date" => $outputQuoteDate ];
$i++;
}
What am I doing wrong here?
I am attempting to return a json object and I can't seem to get past the array... I've built hundreds of regular array and returned them as a json object but I am having a hard time wrapping my head around this one.
$rows = array();
$post_array = array();
$i = 0;
$result = mysql_query(" SELECT * FROM forum_posts WHERE permalink = '$permalink' AND LOWER(raw_text) LIKE '%$str%' " );
while($row = mysql_fetch_assoc($result))
{
$post_array[$i] = $rows[ "id" => htmlentities($row["id"]),
"post_content" => htmlentities($row["content"]),
"author" => $row["author"],
"last_updated" => $row["last_updated"],
"author_id" => $row["author_id"],
"editing_author" => $row["editing_author"],
"date" => $outputQuoteDate ];
$i++;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您的意思是为
$post_array[$i] = ...
定义一个数组。像这样?(此外,为了便于阅读,我只是随意重新排列了一点空间。)
要将数组转换为 JSON,请将其传递给
json_encode()
。更新:哦,在你问这个问题之前,我只是注意到我出于习惯在数组中的最后一项后面添加了一个逗号。看起来好像不太合适,但实际上在定义数组时把它放在那里就很好了。它没有任何特殊用途,但它允许您从数组中复制/粘贴/删除行,而不必担心是否添加/删除尾随逗号。
顺便说一句,您不必手动增加数字数组索引
$i
。如果您只是这样做:它将自动分配下一个可用的数字索引。
It looks like you mean to define an array for
$post_array[$i] = ...
. Like this?(Also, I just took the liberty to respace that a little for readability.)
To convert your array to JSON, pass it to
json_encode()
.Update: Oh, before you ask about it, I just noticed I added a comma out of habit after the last item in the array. It looks like it's out of place, but it's actually fine to have it there when defining arrays. It doesn't serve any special purpose, but it allows you to copy/paste/remove lines from the array without having to worry about whether or not to add/remove a trailing comma.
As an aside, you don't have to manually increment a numeric array index
$i
. If you just do this:it will automatically assign the next available numeric index.
你的意思是做这样的事情:
然后你可以简单地使用 json_encode 将你的数组编码为 json。
例如,
您无法像使用
$rows[...]
那样构建数组,您需要使用 数组。另外,您无需管理数组的序数,只需使用 array_pushDo you mean do be doing something like this:
Then you can simply use
json_encode
to encode your array as json.e.g.
You can't build an array the way you were with
$rows[...]
, you need to use array. Also, instead of managing the ordinals for your array, you can just use array_push