$query = "SELECT Subjects FROM instructors";
$rows = mysqli_query($con,$query);
{
if($rows)
{
$count = 0;
while($row = mysqli_fetch_array($rows))
{
$id = 'Name'.strval($count);
// array_push($results,$row);
$results[] = array($id => $row['Subjects']);
$count = $count + 1;
}
echo json_encode($results);
}
else
{
echo "Something wrong";
}
}
This outputs
[{"Name0":"lore"},{"Name1":"ipsum"}]
Now in JS, I would like to get these names.
$(document).ready(function(){
$.ajax({
url: "get_subs.php",
dataType :"JSON",
success : function(data)
{
alert(data);
}
})
});
But this only prints
0 => [object Object]
I also tried
alert(data.Name0);
But that also alerts the same thing. What am I doing wrong?