如何通过`echo json_encode($ data);`获取所有数据

I want to update user wall by my below script if new data added in sql within last 15 second.

It's update fine if I just use only one data $data = $row['description']; in server.php file.

When I am going to array all data in server.php file to display full post and make my script as below, It's display [object Object] continuously.

So I want to display newly added full post with user img, name, detail etc.

But I have no idea about how to do it.

Please give me a guideline.

server.php

include("../db.php");
global $dbh;
header('Content-Type: application/json; charset=utf-8');
while (true) {
date_default_timezone_set('Asia/Dhaka');
//fetch data 
$datetime = date('Y-m-d H:i:s', strtotime('-15 second'));
$results = mysqli_query($dbh,"SELECT * FROM comments WHERE qazi_id='1012' AND date >= '$datetime' ORDER BY date DESC LIMIT 1") or die(mysqli_error($dbh));
$rows =  mysqli_fetch_assoc($results);

$data = array();

$data['id'] = $rows['id'];
$data['qazi_id'] = $rows['qazi_id'];
$data['likes'] = $rows['likes'];
$data['username'] = $rows['username'];
$data['img'] = $rows['img'];
$data['description'] = $rows['description'];
$data['url'] = $rows['url'];
$data['parent_id'] = $rows['parent_id'];
$data['date'] = $rows['date'];

//has data
if (!empty($data)) {
    echo json_encode($data);
    flush();
    exit(0);
}
sleep(5);
}

JS Script:

function addmsg(type, msg){
    $("#messages").append(
        "<div class='msg "+ type +"'>"+ msg +"</div>"
    );
}

function waitForMsg(){
    $.ajax({
        type: "GET",
        url: "server.php",
        async: true, 
        cache: false,
        timeout:15000, 
        success: function(data){ 
            addmsg("new", data); 
            setTimeout(
                waitForMsg, 
                1000 
            );
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            setTimeout(
                waitForMsg, 
                15000); 
        }
    });
};

$(document).ready(function(){
    waitForMsg(); 
});

Html:

<div id="messages">

</div>

In the "simple" example ($data = $row['description'];) your data is returned as a string, that's why it works. When you sent your object back to the page things get more difficult. You still try to print the object as a string (JavaScript translates and shows you [Object Object], saying that you actually retrieved an object). You need to supply the proper index/key to msg to retrieve the correct info.

For example: msg['description'] should return the description.

function addmsg(type, msg){
    $("#messages").append(
        "<div class='msg "+ type +"'>"+ msg["description"] +"</div>"
    );
}

Basically your data is returned as this

msg = {
         "username" : "foo",
         "description" : "bar",
         etc....
      }

It's returned as a JavaScript object from which you can retrieve properties normally.

  • msg.username or
  • msg["username"]

To build your message you need to build your message object containing the various properties of msg (or data).

function addmsg(type, msg){
    $("#messages").append(
        "<div class='msg "+ type +"'>User " + msg["username"] + "sent: " + msg["description"] +"</div>"
    );
}

You must reference the properties of the object, not the object itself. For example, to show the description:

function addmsg(type, msg){
    $("#messages").append(
        "<div class='msg "+ type +"'>"+ msg.description +"</div>"
    );
}

Alternatively, just pass the description to your function instead:

success: function(data){ 
            addmsg("new", data.description); 
            setTimeout(
                waitForMsg, 
                1000 
            );
        },