MySQL 结果数组...

发布于 2024-12-17 15:35:40 字数 918 浏览 0 评论 0原文

我在这里做错了什么?

我正在尝试返回一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风月客 2024-12-24 15:35:40

看起来您的意思是为 $post_array[$i] = ... 定义一个数组。像这样?

$post_array[$i] = array(
                      "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,
                  );

(此外,为了便于阅读,我只是随意重新排列了一点空间。)

要将数组转换为 JSON,请将其传递给 json_encode()

更新:哦,在你问这个问题之前,我只是注意到我出于习惯在数组中的最后一项后面添加了一个逗号。看起来好像不太合适,但实际上在定义数组时把它放在那里就很好了。它没有任何特殊用途,但它允许您从数组中复制/粘贴/删除行,而不必担心是否添加/删除尾随逗号。


顺便说一句,您不必手动增加数字数组索引 $i。如果您只是这样做:

$post_array[] = array(...);

它将自动分配下一个可用的数字索引。

It looks like you mean to define an array for $post_array[$i] = .... Like this?

$post_array[$i] = array(
                      "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,
                  );

(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:

$post_array[] = array(...);

it will automatically assign the next available numeric index.

回忆那么伤 2024-12-24 15:35:40

你的意思是做这样的事情:

$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] =array(  "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++;
    }

然后你可以简单地使用 json_encode 将你的数组编码为 json。

例如,

echo json_encode($post_array);

您无法像使用 $rows[...] 那样构建数组,您需要使用 数组。另外,您无需管理数组的序数,只需使用 array_push

Do you mean do be doing something like this:

$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] =array(  "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++;
    }

Then you can simply use json_encode to encode your array as json.

e.g.

echo json_encode($post_array);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文