1

This is the output:

[
    {
        "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." 
    }
]

As you may see there are repeated records added to an integer, and the row name, this is how it works:

// SQL statement and mysql_query

$posts = array();

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

How do you get it to not repeat twice, just show the row name and the record.

3 Answers 3

4

You're using fetch-array: http://php.net/manual/en/function.mysql-fetch-array.php

That uses the default ('both'), but you want to specify MYSQL_ASSOCor MYSQL_NUM, and you'll get just the one :)

Sign up to request clarification or add additional context in comments.

4 Comments

I'm really interested why this would get a -1?
@Nanne Also love to know why my answer was downvoted as well. Interesting that only one of them hasn't been.
Because my answer was better ;P meta.stackexchange.com/questions/17204/…
@Petah Downvoting correct answers isn't a valid response. (That said, there are scripts than run periodically that analyse voting patterns and adjust things accordingly.)
3

An easy way to do this is to use the MYSQL_ASSOC option with the mysql_fetch_array function as follows:

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

Using this option will return the "named" keys (you can use MYSQL_NUM if you simply want the numeric keys). Alternatively, you can simply use the mysql_fetch_assoc function in place of the mysql_fetch_array, as in:

while($row = mysql_fetch_assoc($result))

Comments

1

Use either

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

or

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

The reason you are getting that is because mysql_fetch_array returns both an associative array, and a numeric array

3 Comments

fetch_object has a different effect then fetch_array, so that's maybe changing the wanted effect too much. fetc-assoc is equivalent to fetch_array using one of the optional second params.
@Nanne, when you json_encode either of those 2 you get the same result. And yes, like you said, your answer did require more syntax and need parameters than mine, I agree. But isn't that a bad point rather than a good point?
Neither. it really doesn't matter for any sane definition of matter. That's the whole point why there is no real difference between the two answers. Note that I didn't downvote your answer. But being this is silly, i wish a good day :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.