Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MySQL结果数组。。。_Php_Mysql_Arrays - Fatal编程技术网

Php MySQL结果数组。。。

Php MySQL结果数组。。。,php,mysql,arrays,Php,Mysql,Arrays,我做错了什么 我试图返回一个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(

我做错了什么

我试图返回一个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++;
    }

看起来您的意思是为
$post\u数组[$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,请将其传递给

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


另外,您不必手动递增数值数组索引
$i
。如果您只是这样做:

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

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

看起来您的意思是为
$post\u 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,请将其传递给

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


另外,您不必手动递增数值数组索引
$i
。如果您只是这样做:

$post_array[] = 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] =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\u encode
将数组编码为json

e、 g


您不能像使用
$rows[…]时那样构建数组,您需要使用。另外,您也可以使用

来代替管理数组的序号。您的意思是要执行以下操作:

$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\u encode
将数组编码为json

e、 g

您不能像使用
$rows[…]时那样构建数组,您需要使用。此外,您可以使用