Nicero Nicero - 11 months ago 48
PHP Question

Access mySQL fields using Ajax and PHP

After lot of temptatives and after reading lot of posts about I'm still unable to read mySQL fields using Jquery. My PHP code, which connects to a db and gets some data, outputs the following using

echo json_encode($html);


I then try to read the single fields but I keep getting "undefined". For example:

$.getJSON("get.php", function(result) {
$.each(result, function(key, field){

If I simply use
instead of
it works but I get all the fields (of course).


Your JSON data contains only a single object, not an array of objects.

You are iterating over each property within this JSON object with $.each, so within that callback function key will be the property name and field will be the property value.

Change your console log call to this:


and you can see the difference, the value will of the property will be logged.

You can access the properties of the JSON directly like so:

$.getJSON("get.php", function(result) {
     // etc