Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 循环中的JSON_Php_Json - Fatal编程技术网

Php 循环中的JSON

Php 循环中的JSON,php,json,Php,Json,我拥有的代码执行得很好,但在我包含了向我建议的循环之后,问题是它检索的数据只显示[Object] 这是我目前的进展 $sql6="SELECT msgid FROM thread WHERE combination1=:msgids OR combination2=:submsgids"; $msg_id = $con4->prepare($sql6); $msg_id->bindParam(':msgids', $comb, PDO::PARAM

我拥有的代码执行得很好,但在我包含了向我建议的循环之后,问题是它检索的数据只显示[Object]

这是我目前的进展

      $sql6="SELECT msgid FROM thread WHERE combination1=:msgids OR combination2=:submsgids";
      $msg_id = $con4->prepare($sql6);
      $msg_id->bindParam(':msgids', $comb, PDO::PARAM_STR);
      $msg_id->bindParam(':submsgids', $comb, PDO::PARAM_STR);
      $msg_id->execute();
      $msgd = $msg_id->fetchColumn();
      $tbpre = $msgd;
    $sql7 = "SELECT   message_content, username , message_time, recipient FROM ".$tbpre."chat_conversation WHERE msgid=:chat";

    $stmt7=$con3->prepare($sql7);
    $stmt7->bindValue( 'chat', $msgd, PDO::PARAM_STR);
    $stmt7->execute();

  $message_query = $stmt7;

$json = array();

if($message_query->rowCount() > 0) {
    while($message_array = $stmt7->fetchAll(PDO::FETCH_ASSOC)) {
        $json[] = $message_array;
    }
    echo json_encode($json);
}
这是我的JS

    function AjaxRetrieve()
        {
          var rid = document.getElementById('trg').value,
    data = {chat: uid, rid: rid, name: user};

$.get('includes/getChat.php', data, function (result) {
    var res = $([]);

    $.each(result[0], function(key, value) {
        res = res.add($('<div />', {text : value}));
    });

    $("#clog").html(res);

}, 'json');
        }
res是一个jQuery对象,由$[]创建,并由res.add填充

您正在将一个对象输出到字符串上下文HTML中,因此它当然会产生[object object]——这就是对象转换为字符串的方式

考虑一下。。。在不需要jQuery的地方不使用它

var res = [], out = document.getElementById('clog'), div;
while(out.firstChild) out.removeChild(out.firstChild);
$.each(result[0], function(key, value) {
    div = document.createElement("div");
    div.appendChild(document.createTextNode(value});
    res.push(div);
    out.appendChild(div);
}

首先,在php端检查输入数组。并展示它。第二步是重新检查手动打开和查看深度参数


如果您发送了错误的params/array/json,JS端将不会有帮助

您将获得[Object Object]作为对象的数组。请发布你的js代码。好的。。我已经发布了=嗨,我试过了,可惜还是一样,结果给了我[object]=