0
votes

I made a PHP script:

function getData(){

  $DBH = connectDB(********);

  $STH = $DBH->query('SELECT * FROM table');

  $tData = $STH->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode($tData);

}

This is executed in getData.php. The fields are ID, name, lastname, age. Basically, get's all the table's values and put them in a JSON array.

I dont know how to complete the JQuery's getJSON function to retrieve, for example, the ID 5 register or the name = "John" one.

The examples at JQuery.com are a bit complex due my little understanding of how all this stuff works.

2

2 Answers

1
votes

$.getJSON("you page.php", { format: "json" }, function(data) { $.each(data.items, function(i,item){ Alert(item["yourfildname"]); }); });

1
votes

UPDATED:

$(function() {
    $.getJSON('getData.php',function(data) {
        $.each(data,function(i, item) {
            alert(item.ID + '->' + item.name + '->' + item.lastname );
        });
    });
});

NB: the way on how you get the single ID, NAME depend on how the json_encode($tData) is formatted, so, you do better if you post to us the echoed json!