I have a database in MySQL
which I want to visualize in D3.JS
. In order to do that, first I want to parse
the data in JSON
fo
I have faced problem myself with json_encode, in the end I wrote my own code instead of json_encode. Just you need to set the sql and yoru connection info. Hope you find it useful.
$conn = new mysqli(DB_Host, DB_User, DB_Password, DB_Name) or die("Connection failed: " . $conn->connect_error);
$sql = "select AID as id, deDate_Value as date, nterTime AS counter
from xxxx";
//Get the Date and store it in array
$records = array();
if ( $result=mysqli_query($conn,$sql) )
while ( $obj=mysqli_fetch_object($result) )
$records[] = $obj;
//echo in json format on screen
echo "[";
$comma_1 = "";
foreach($records as $obj)
{
echo $comma_1."{";
$comma_2 = "";
foreach($obj as $key => $value)
{
echo $comma_2.'"'.$key."\": \"". $value . "\"";
$comma_2 = ", ";
}
echo "}";
$comma_1 = ", \n";
}