Ajax和PHP有一个while循环问题。

时间:2022-11-24 14:27:06

I have a ajax that needs to return some values, but if I use a while() to get all the result from my db, the return is "nothing". Where am I making mistake?

我有一个需要返回一些值的ajax,但是如果我使用while()从我的db中获取所有结果,那么返回值是“没有”。我哪里出错了?

My Ajax script is posted below:

我的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>

My php script is posted below:

我的php脚本如下:

 $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);

3 个解决方案

#1


4  

You are overwriting your results variable in the loop:

在循环中覆盖结果变量:

while (...) {
  ...
  $res=[];
  ...
}

You probably want something like:

你可能想要这样的东西:

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

#2


0  

Overwrite your value with same index. you can write this.

用相同的索引覆盖你的值。你可以写下来。

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);

die();

die();

Write your js addmsg() function.

编写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);
    }
  }

#3


-1  

re-write your script

重写你的脚本

function waitForMsg(){
$.get("notification/select.php",function(callback){
console.log(callback); // here is your data in callback variabel you can in in console log
})
}

Replce this code with your php script.

用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);

#1


4  

You are overwriting your results variable in the loop:

在循环中覆盖结果变量:

while (...) {
  ...
  $res=[];
  ...
}

You probably want something like:

你可能想要这样的东西:

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

#2


0  

Overwrite your value with same index. you can write this.

用相同的索引覆盖你的值。你可以写下来。

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);

die();

die();

Write your js addmsg() function.

编写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);
    }
  }

#3


-1  

re-write your script

重写你的脚本

function waitForMsg(){
$.get("notification/select.php",function(callback){
console.log(callback); // here is your data in callback variabel you can in in console log
})
}

Replce this code with your php script.

用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);