How can I do JSON pretty print from MySQL? I used JSON_PRETTY_PRINT
in my code but it is not printing what I am expecting. My current script is:
<?php
//open connection to mysql db
$connection = mysqli_connect("127.0.0.1","root","Kunal@7890","testdb") or die("Error " . mysqli_error($connection));
//fetch table rows from mysql db
$sql = "select id,title,profilepic,created_at,url from news";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
print json_encode($rows, JSON_PRETTY_PRINT);
}
?>
for this script I am getting a result like:
[ { "id": "1", "title": "test", "profilepic": "0", "created_at": "2016-09-05 12:11:17", "url": "0" } ][ { "id": "1", "title": "test", "profilepic": "0", "created_at": "2016-09-05 12:11:17", "url": "0" }, { "id": "2", "title": "JCECE", "profilepic": "http:\/\/results.jharkhandeducation.net\/JCECEB\/JCECEB-Logo.jpg", "created_at": "2016-09-16 10:14:55", "url": "https:\/\/jcece.co.in\/" } ]
I want my result to be printed with the table name first followed by columns, something like this:
{
"news": [
{
"id": 36,
"name": "JCECE",
"image": null,
"status": " JCECE 2016 will be conducted on June 5, 2016 by JCECE Board, which is the exam conducting authority for the engineering entrance examination. JCECE 2016 will be conducted to offer admissions to undergraduate engineering courses at the participating institutes of JCECE 2016 in the state of Jharkhand. As of now, there are a total of 19 colleges (government+private) that will offer over 6000 B.E/B.Tech seats to aspiring candidates in Jharkhand.
Application Dates:16 Apr 2016 to 16 May 2016
Admit Card Date:11 May 2015
Exam Dates:05 Jun 2016
Result Date:01 Jul 2015 to 10 Jul 2015 ",
"profilePic": "http://results.jharkhandeducation.net/JCECEB/JCECEB-Logo.jpg",
"timeStamp": "1461323436930",
"url": "https://jcece.co.in/"
},
{
"id": 39,
"name": "THAPAR UNIVERSITY",
"image": null,
"status": "The details about the Thapar University B.Tech admission 2016 have been released. The admission will be held as per the JEE Main 2016 score but candidates will have to fill a separate application form for it. Interested candidates, who are also eligible, may access the link below to apply. The last date to submit the application form is 26 April 2016.
Last Date:26 Apr 2016",
"profilePic": "https://upload.wikimedia.org/wikipedia/commons/d/da/ThaparUniversityLogo.jpg",
"timeStamp": "1459595788930",
"url": "http://www.thapar.edu/"
},
]