2

here's my php code

$result = mysql_query("select * from backup where owner='$email'") or die (mysql_error());
$dataCount = mysql_num_rows($result);
$row = mysql_fetch_array($result);
echo json_encode($row);

and it returns this:

{"0":"1","id":"1","1":"2015","year":"2015","2":"55","necessities":"55","3":"10","savings":"10","4":"10","entertainment":"10"}

this is how jsonviewer.stack.hu shows it jsonviewer

fyi, there's only one row of data inside the table. but it seems json_encode($row) displays the value twice, but firstly using number (0 - 4) as the label, then it uses the column name (id, year, necessities, savings, entertainment) as the label.

how can I make it to display the value only once, using the column name?

2

3 Answers 3

4

Change mysql_fetch_array to mysql_fetch_assoc.

mysql_fetch_array returns a result row in both numeric and associative array.

mysql_fetch_assoc returns a result row as associative array.

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

1 Comment

awesome!! and thanks for the explanation! I'd accept your anwer in the next 10 minutes.
3

http://php.net/manual/en/function.mysql-fetch-array.php

You can give this functional additional arguments including MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH. In your case you want MYSQL_ASSOC.

However, you should be using mysqli and not mysql. the mysql functions are no longer maintained.

http://php.net/manual/en/mysqli-result.fetch-array.php

1 Comment

You can also use mysql_fetch_assoc() instead of giving MYSQL_ASSOC argument. (Same for mysqli_* functions.)
2

For new versions of php ( > 5.5), use mysqli_fetch_assoc instead of mysql_fetch_assoc. The mysql_fetch_assoc function is deprecated and is no longer maintained: Using it will lead to errors.

Comments

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.