开发者

getJSON and retrieving data from an Array

开发者 https://www.devze.com 2023-01-28 19:46 出处:网络
I made a PHP sc开发者_运维知识库ript: function getData(){ $DBH = connectDB(********); $STH = $DBH->query(\'SELECT * FROM table\');

I made a PHP sc开发者_运维知识库ript:

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.


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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号