0

PHP: $result = mysql_query("SELECT * FROM events LIMIT 15");

Can anyone help and show me how create json that looks exactly like the one below from the php query above?

{
"events": [
    {
     "id": 2,
        "name": "TIME",
        "image": "http://127.0.0.1/android_tests_json_and_xml/images/time_best.jpg",
        "status": "30 years of Cirque du Soleil's best photos",
        "profilePic": "http://127.0.0.1/android_tests_json_and_xml/images/time.png",
        "timeStamp": "1403375851930",
        "url": "http://ti.me/1qW8MLB"
    },
    {
        "id": 3,
        "name": "Discovery",
        "image": "http://127.0.0.1/images/discovery_mos.jpg",
        "status": "A team of Austrian scientists has developed a laser system that causes fruit flies to dance.",
        "profilePic": "http://127.0.0.1/android_tests_json_and_xml/images/discovery.jpg",
        "timeStamp": "1403375851930",
        "url": "http://dsc.tv/xmMxD"
    },
    {
        "id": 11,
        "name": "A. R. rahman",
        "image": "http://127.0.0.1/images/ar_bw.jpg",
        "status": "",
        "profilePic": "http://127.0.0.1/android_tests_json_and_xml/images/ar.jpg",
        "timeStamp": "1403375851930",
        "url": ""
    }
]

}

I'd really appreciate it

1
  • edit with more of your code Commented Apr 3, 2015 at 10:14

3 Answers 3

2

try like this,

$rows = array();
$result = mysql_query("SELECT * FROM events LIMIT 15");
while($row = mysql_fetch_assoc($result)) {
 $row['events'][] = $row;
}
echo  json_encode($rows);
Sign up to request clarification or add additional context in comments.

Comments

1
$sth = mysqli_query("SELECT ...");
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
 $rows[] = $r;
}
return json_encode($rows);

Comments

0

try with class and json serialization interface

class Event implements JsonSerializable
{
        var $id;
        var $name;
        var $image;
        var $status;
        var $profilePic;
        var $timeStamp;
        var $url;
        public function __construct($id , $name , $image , $status , $profilePic , $timeStamp , $url) {



            $this->id=$id 
            $this->name=$name 
            $this->image=$image 
            $this->status=$status 
            $this->profilePic=$profilePic;
            $this->timeStamp=$timeStamp; 
            $this->url=$url;
        }

        public function jsonSerialize() {

        return [
            'name' => $this->name,
            'message' => $this->message,
            'id'=> $this->id,
            'name'=>$this->name,
            'image'=>$this->image,
            'status'=>$this->status,
            'profilePic'=>$this->profilePic,
            'timeStamp'=>$this->timeStamp,
            'url'=>$this->url
         ];
    }
}


$events=array();

$result = $db->query("SELECT * FROM events LIMIT 15");

if ($result) {

        while($row = $result->fetch_assoc()) {

            array_push($events,new Event($row['id'] , $row['name'] , $row['image'] , $row['status'] , $row['profilePic'] , $row['timeStamp'] , $row['url']));
        }

}
else {

    $events=null;
}
$db->close();

if($events==null)
{
    echo "data not found";
}
else
{
    echo json_encode($events);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.