Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8540711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:39:51+00:00 2026-06-11T11:39:51+00:00

I have some PHP that generates the a JSON object from data from a

  • 0

I have some PHP that generates the a JSON object from data from a MySQL database

$addressData = mysql_query("SELECT * FROM address WHERE ContactID = $contactID")or  die("<br/><br/>".mysql_error());

while($r = mysql_fetch_assoc($addressData)){
    $rows[] = array('data' => $r);
}

// now all the rows have been fetched, it can be encoded
echo json_encode($rows);

This generates the following JSON object:

[
  {"address":
    {"AddressID":"10011","AddressType":"Delivery","AddressLine1":"4 Caerleon Drive","AddressLine2":"Bittern","AddressLine3":"","CityTown":"Southampton","County":"Hampshire","PostCode":"SO19 5LF","Country":"United Kingdom","ContactID":"10011"}},
  {"address":
    {"AddressID":"10012","AddressType":"Home","AddressLine1":"526 Butts Road","AddressLine2":"Sholing","AddressLine3":"","CityTown":"Southampton","County":"Hampshire","PostCode":"SO19 1DJ","Country":"England","ContactID":"10011"}}
]

When receiving it back in Ajax and running it through the following:

$.each(data, function(key, val) {
  string =string + "Key: " + key + " Value:" + val + "<br />";
});

which prints the following:

Key: 0 Value:[object Object]

Key: 1 Value:[object Object]

Any ideas about how I can access the objects within key 0 and 1 in data?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:39:52+00:00Added an answer on June 11, 2026 at 11:39 am

    This is because Object’s default toString implementation results in “[object Object]” when concatenating to a String object. You can access the fields of the val object as usual, for example:

    val.data.AddressID
    

    like this:

    $.each(data, function(key, val) {
      string += "Key: " + key + " Value:" + val.data.AddressID + "<br />";
    });
    

    Please note that val in the loop above is the {“data”: …} part of the JSON code, that is why you need to specify val.data. to access parts of the address data inside.

    Also, you could simply remove the data nesting level, resulting in a JSON layout like that:

    [
        {"AddressID":"10011","AddressType":"Delivery","AddressLine1":"4 Caerleon Drive","AddressLine2":"Bittern","AddressLine3":"","CityTown":"Southampton","County":"Hampshire","PostCode":"SO19 5LF","Country":"United Kingdom","ContactID":"10011"},
        {"AddressID":"10012","AddressType":"Home","AddressLine1":"526 Butts Road","AddressLine2":"Sholing","AddressLine3":"","CityTown":"Southampton","County":"Hampshire","PostCode":"SO19 1DJ","Country":"England","ContactID":"10011"}
    ]
    

    Using this PHP code:

    while($r = mysql_fetch_assoc($addressData)){
        $rows[] = $r;
    }
    

    And then, you could access the address data fields in the loop like this:

    $.each(data, function(key, address) {
      string += "Key: " + key + " Value:" + address.AddressID + "<br />";
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some PHP code that generates dynamic tables of data on the fly.
I have some php code that generates multiple MySql queries and combines the results.
I have a PHP script that generates an RSS feed using some external data
I have some PHP code that generates a calendar and then outputs html to
I have a PHP web app that generates a somewhat complex report from our
I have some php code that generates a random password. Next to a text
I have created simple web service for my website that generates some json based
I have a php script that generates a binary file (is some kind of
I am retrieving json from a php file that connects to the database and
I have some PHP code that is designed to make a spreadsheet with formulas

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.