I'm trying to simply echo a JSON object literal which includes every row in my database table.
As standard with the methods found in many other similar questions I looked at, I'm fetching every row and encoding it like so:
$con=mysqli_connect("localhost","username","pass","markers");//markers is db table name
$result = mysqli_query($con,"SELECT * FROM markers");
$rows = array();
while($r = mysqli_fetch_array($result))
{
$rows[] = $r
//or: $rows[] = array('data' => $r);
}
echo json_encode($rows);
Similarly to the question PHP: can't encode json with multiple rows or mysql table to json ,
I want to echo a JSON Object which looks like this:
{
"data1": // how do I get "data1"?
{
name: John Smith,
title: Mr,
description: a man
}
}
{
"data2":{ // how do I get "data2"?
name:Bob Smith,
title: Mr,
description: another guy
}
}
Except I do not get how to achieve the "headers", the titles of the first level string of objects, such as "data1" or data2". In fact, my database table doesn't necessarily even have those values/that column.
This is what it looks like right now:
How can I get simply numerical "headers" like "1", "2" or "data1", "data2" without having to designate the "name" column as the "headers"? (Or do I have to have a column for that?)
My goal is to process the values in every "row" in the returned JSON.
I plan to use the jQ $.each function $.each(data, function(dataNum, dataInfo)
-- e.g. data1 would be passed to dataNum and the values would be passed to dataInfo -- and be able to access specific table values by dataInfo.name
, which right now is not working.
Thanks to any help in advance!