JSON объект повторяется в выводе PHP - PullRequest
1 голос
/ 06 февраля 2011

Это вывод:

[
    {
        "0": "1",
        "cat_id": "1",
        "1": "Hello World!",
        "post_title": "Hello World!",
        "2": "1296943703",
        "post_date": "1296943703",
        "3": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
        "post_content": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." 
    }
]

Как вы можете видеть, к целому числу добавляются повторяющиеся записи и имя строки, вот как это работает:

// SQL statement and mysql_query

$posts = array();

while($row = mysql_fetch_array($result))
{
    $posts[] = $row;
}
mysql_free_result($result);
die(json_encode($posts));

Как сделать так, чтобы оно не повторялось дважды, просто покажите имя строки и запись.

Ответы [ 3 ]

4 голосов
/ 06 февраля 2011

Вы используете fetch-array: http://php.net/manual/en/function.mysql-fetch-array.php

Это использует значение по умолчанию ('оба'), но вы хотите указать MYSQL_ASSOC или MYSQL_NUM, и вы получите толькоодин:)

3 голосов
/ 06 февраля 2011

Простой способ сделать это - использовать опцию MYSQL_ASSOC с функцией mysql_fetch_array следующим образом:

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

Использование этой опции вернет "именованные" клавиши (вы можете использовать MYSQL_NUM, если вам просто нужны цифровые клавиши). В качестве альтернативы вы можете просто использовать функцию mysql_fetch_assoc вместо mysql_fetch_array, например:

while($row = mysql_fetch_assoc($result))
1 голос
/ 06 февраля 2011

Используйте либо

mysql_fetch_object см. http://php.net/manual/en/function.mysql-fetch-object.php

или

mysql_fetch_assoc см. http://php.net/manual/en/function.mysql-fetch-assoc.php

Причина, по которой вы получаете этопотому что mysql_fetch_array возвращает как ассоциативный массив, так и числовой массив

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...