Firstly i'll point out the problem, basically inside my $.each(data, function (i, v) { code it's returning a bunch of variables which are echo'd from the PHP code which is using json_encode but when trying to place these variables or even alert() them it's showing undefined i've tried many ways to actually display the data, but it's always returning undefined and i don't know why since my code seems valid to my perspective.
The current javascript code i have is as follows
$.ajax({
url: "functions/ajax.php",
data: "func=auto",
type: "GET",
dataType: "json",
success: function(data){
$.each(data, function (i, v) {
var name = v['name'];
var player_id = v['id'];
alert(player_id);
});
},
error: function (jqXHR, textStatus, errorThrown){
console.log('Error ' + jqXHR);
}
});
The current PHP code i have is as follows
$res = $DB->Query("SELECT * FROM `inventory` WHERE `account_id` = '$_SESSION[ID]'");
$data = array();
while($player = $DB->fetch_assoc()) {
$data['name'] = $player['name'];
$data['id'] = $player['player_id'];
}
header('Content-type: application/json');
echo json_encode($data);
Just to sum the whole thing up, when using alert() on player_id it returns undefined in which obviously i want it to return the correct value
.each()over it. Or run$data[] = array('name'=>$player['name'],'id'=>$player['player_id']);while($player = $DB->fetch_assoc()) {is returning more than one result ?$datais in your current script. It will only be last player in the list unless you append records to$datainstead of overwriting it.