将连接数据从mysql输出到json作为json对象

时间:2016-05-02 19:26:30

标签: php mysql json

这是我的数据库:

enter image description here

我试图将所有from_id,其中to_id为4,这是一个json对象,其中ill可以显示来自堆栈中from_id的所有消息,这些消息按其不同的time_sent排序

我试过了:

CODE: 数据库查询类(这是在DBASE类中)

public function query($sql){    

 $this->_last_query = $sql;
 $result = mysqli_query($this->_conndb, $sql);
 $this->displayQuery($result);
 return $result;    

} // end of query

public function displayQuery($result){
if(!$result){
    $output = "database query failed :". mysqli_error($this->_conndb);
    $output .= "last sql query was: ".$this->_last_query;
    die($output);       
    }   
    else{
        $this->_affected_rows = mysqli_affected_rows($this->_conndb);

        }
} //End of query results

public function fetchAll($sql){

    $result =  $this->query($sql);
    $out =  array();
    while($row =  mysqli_fetch_assoc($result)){
        $out[] =  $row;
    }
    mysqli_free_result($result);
    return $out;
}   

MESSAGE CLASS:

private $_table = 'messages';
public function getadminMessage(){
     $sql = "SELECT group_concat(messg, time_sent, from_id) as from_id from {$this->_table} where to_id = 4 group by from_id"; 
    return $this->db->fetchAll($sql);//IN THE DBASE CLASS ABOVE

    }

在文件getadminmessage.php

    $message = new Message();
$results  = $message-> getadminMessage();
echo json_encode($results);

然后我使用getjson

$.getJSON("http://127.0.0.1/tum_old/custom/php/getadminmsg.php",

function(response){
console.log(response);

});

在我的日志中我得到了

enter image description here

数据似乎是串联的,但我也希望各种消息都是对象形式,即要显示为对象的消息,这样我就可以轻松完成:

data.object [0] .from_id [0] .msg获取TRUE LOVE作为消息

scais ::::

enter image description here

3 个答案:

答案 0 :(得分:1)

试试这个:

   public function fetchAll($sql){

    $result =  $this->query($sql);
    $out =  array();
    while($row =  mysqli_fetch_assoc($result)){

        $out[] = array(
            'id' => $row['id'],
            'from_id' => $row['from_id'],
            'to_id' => $row['to_id'],
            'msg' => $row['messg']
        );
    }
    mysqli_free_result($result);
    return json_encode($out);
    }

答案 1 :(得分:1)

尝试删除group by语句并使用结果行构建新的多维数组,然后将其作为json编码的字符串输出。见下面的例子

$out = array(
    'messages' => array(),
);
while($row =  mysqli_fetch_assoc($result)){
    $out['messages'][$b['from_id']][] = array(
        'msg' => $b['message'],
        'time_sent' => $b['time_sent'],
    );
}
return $out;

$ out ['messages']数组中的键将是你的from_id值。每个条目都是来自每个id的消息数组,因此在您的javascript中,您应该可以迭代这些

答案 2 :(得分:1)

选择你需要的

$sql = "SELECT group_concat(messg) as msg, time_sent, from_id
  from {$this->_table} where to_id = 4 group by from_id"; 

对于服务器端,您需要json_encode数组

public function fetchAll($sql){

  $result =  $this->query($sql);
  $out =  array();
  $cnt =0;
  while($row =  mysqli_fetch_assoc($result)){
    $out[$cnt]['msg'] =  $row['msg'];
    $out[$cnt]['time_sent'] =  $row['time_sent'];
    $out[$cnt]['from_id'] =  $row['from_id'];

    $cnt++;
  }
  mysqli_free_result($result);
  return json_encode($out);
}
相关问题