Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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
带有while循环问题的Ajax和PHP_Php_Jquery_Ajax - Fatal编程技术网

带有while循环问题的Ajax和PHP

带有while循环问题的Ajax和PHP,php,jquery,ajax,Php,Jquery,Ajax,我有一个ajax,它需要返回一些值,但是如果我使用while()从数据库中获取所有结果,则返回“nothing”。我哪里出错了 我的Ajax脚本发布在下面: <script type="text/javascript" charset="utf-8"> function addmsg(type, msg) { var obj = jQuery.parseJSON(msg); // Your count variable var count = obj.co

我有一个ajax,它需要返回一些值,但是如果我使用
while()
从数据库中获取所有结果,则返回“nothing”。我哪里出错了

我的Ajax脚本发布在下面:

<script type="text/javascript" charset="utf-8">
  function addmsg(type, msg) {
    var obj = jQuery.parseJSON(msg);
    // Your count variable
    var count = obj.count;
    // Your not variable
    var not = obj.not;
    $('#msg_count').html(count);
    $('#notification').html(not);

  }

  function waitForMsg() {
    $.ajax({
      type: "GET",
      url: "notification/select.php",
      cache: false,
      timeout: 50000,

      success: function(data) {
        addmsg("new", data);
        setTimeout(
          waitForMsg,
          1000
        );
      },
      error: function(XMLHttpRequest, textStatus, errorThrown) {
        addmsg("error", textStatus + " (" + errorThrown + ")");
        setTimeout(
          waitForMsg,
          15000);
      }
    });
  };

  $(document).ready(function() {

    waitForMsg();

  });
</script>
 $result = mysqli_query($con, "SELECT * from notification where tousername='$tousername' and isread = 0");

 while ($row = mysqli_fetch_array($result)) {
     $count = $result - > num_rows;
     $not = $row['notification_msg'];
     $res = [];
     $res['count'] = $count;
     $res['not'] = $not;
 }
 echo json_encode($res);

您正在覆盖循环中的结果变量:

while (...) {
  ...
  $res=[];
  ...
}
您可能需要以下内容:

$res['count'] = $result->num_rows;
while($row = mysqli_fetch_array($result)) {
  $res[]['not'] = $row['notification_msg'];
}
echo json_encode($res);

用相同的索引覆盖您的值。你可以写这个

while($row = mysqli_fetch_array($result)) {
$count = $result->num_rows;
$not=$row['notification_msg'];
$res=[];
$res['count'][] = $count;
$res['not'][] = $not; 
}

Or

while($row = mysqli_fetch_array($result)) {
$count = $result->num_rows;
$not=$row['notification_msg'];
$res=[];
$res[]['count'] = $count;
$res[]['not'] = $not; 
}

echo json_encode($res);
模具()

编写js
addmsg()
函数

function addmsg(type, msg) {
    var obj = jQuery.parseJSON(msg);
    // Your count variable
    for(prop in obj){
      var count = obj[prop][count];
      var not = obj[prop][not];
       $('#msg_count').html(count);
       $('#notification').html(not);
    }
  }
重新编写脚本

function waitForMsg(){
$.get("notification/select.php",function(callback){
console.log(callback); // here is your data in callback variabel you can in in console log
})
}
用php脚本回复此代码

 $result = mysqli_query($con, "SELECT * from notification where tousername='$tousername' and isread = 0");

       $res['count'] = $result->num_rows;
    while($row = mysqli_fetch_array($result)) {
      $res[]['not'] = $row['notification_msg'];
    }
    echo json_encode($res);

它真的返回“无”吗(它没有返回任何内容。没有错误。它没有返回任何内容。@SanjuMenon,
$tousername
来自哪里,当您直接在浏览器中打开
notification/select.php
时,您是否得到任何结果?它来自数据库。我正在将其与登录用户进行比较。@SanjuMenon您应该添加该代码并告诉我们在浏览器中打开
notification/select.php
时会发生什么情况。是的,我在select.php中获得了正确的输出。这是结果…{“count”:3,“0”:{“not”:“New Intender details entered 1”},“1”:{“not”:“New Intender details entered 2”},“2”:{“not”:“New Intender details entered 3”}我需要更改ajax脚本中的任何内容吗?例如..添加一个each..我很抱歉,它没有返回任何内容。只是空白。只是看看我是否犯了错误基本上我和fb一样执行通知系统。它显示未读消息的计数。如果我们单击计数,它显示未读消息Write die()echo json_encode($res)的函数结尾;我的ajax脚本呢?我需要更改什么吗?是的url很好。但是我需要在ajax中添加$。每个都可以容纳while循环吗?到目前为止,我的ajax脚本不是为了处理循环而编写的。对吗?我对ajax非常陌生。是的,您的脚本还可以。但是您可以使用$。get(“notification/select.php”,函数(callback){“您可以在javascript中循环回调变量”}),您是不是直接复制了我的答案,但没有解释?