Share the post "PHP MySQL Result json_encode Includes Value And Its Column Index As Key"
While this is not really an error, extra data that is not used should not be included in any output as it will only add to the extra bytes.
When you look at your JSON output, you realized the column names and values also have extra index column numbers and the associated values included.
To remove this, simply add an option in the PDO when you instantiate a database connection in PHP like this:
|
1 2 3 4 |
<?php $db = new PDO("mysql:host=localhost; dbname=mydbname", "myusername", "mypassword"); $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); ?> |
That should fix the problem. When you use json_encode(), there will be no more duplicate values with the corresponding column index number as key.