如何在数组内创建数组以返回json_encode($ data)

时间:2015-03-12 11:10:18

标签: php arrays

我从页面发送了许多动态post id,并且php服务器端页面(server.php)使用这些id进行查询以在mysql中找到新的插入数据。

我对所有类型的阵列知之甚少。

所以我使用下面的脚本显示在我的网络文件夹error_log "PHP Warning: Invalid argument supplied for foreach() in public_html/server.php on line 80"

请帮我搞定。

server.php

while (true) {
    if($_REQUEST['CID']){  //cid got all dynamic post id as: 1,2,3,4 etc.
      foreach($_REQUEST['CID'] as $key => $value){

        $datetime = date('Y-m-d H:i:s', strtotime('-15 second'));
        $res = mysqli_query($dbh,"SELECT * FROM reply WHERE qazi_id=".$_REQUEST['tutid']."  AND date >= '$datetime' ORDER BY id DESC LIMIT 1") or die(mysqli_error($dbh));

        $rows =  mysqli_fetch_assoc($res);

          foreach($rows as $row){

          $data = array();
          $data['id'] = $rows['id']; 
          $data['qazi_id'] = $rows['qazi_id'];
          $data['username'] = $rows['username'];
          $data['description'] = $rows['description'];
          $data['date'] = $rows['date'];
          //etc. all
             $id = $rows['id'];
             $qazi_id = $rows['qazi_id'];
             $username = $rows['username'];
             //etc. all
          } //foreach close
      } //foreach close

          // do somethig

           if (!empty($data)) {
              echo json_encode($data);
              flush();
              exit(0);
           }

    } //request close
    sleep(5);
} //while close

2 个答案:

答案 0 :(得分:0)

以这种方式使用

 while($row = mysql_fetch_assoc($result)){
      $product = array();
      $product["pid"] = $row["pid"];
      $product["productname"] = $row["productname"];        
    }

   $response["product"] = $product;

答案 1 :(得分:0)

按以下方式更改您的代码

while($rows =  mysqli_fetch_assoc($res))
{
$data[]=$rows;
}
echo  echo json_encode($data);
相关问题