Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8661457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:30:09+00:00 2026-06-12T16:30:09+00:00

I tried to modified the long polling code to fit my situation. I would

  • 0

I tried to modified the long polling code to fit my situation.
I would like to count the total number from mysql and if the number changes (increase or decrease), it will notify me.

The problem is the while loop keep looping. How can I stop it? I just need to have notification whenever the total sum change.

<?php
include("db.php");
$result = mysql_query("SELECT sum( r ) as totalsum FROM (SELECT colA, colB, COUNT( * ) AS r FROM product GROUP BY productid ) AS t");
while($row = mysql_fetch_array($result))
{
    $old_totalsum = $row['totalsum']; 
}

?>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">

var old_totalsum =<?php echo $old_totalsum; ?>;
function waitForMsg(){
$.ajax({
    type: "GET",
    url: "poll.php?old_totalsum=" + old_totalsum,
    async: true,
    cache: false,
        success: function(data){

        var json = "eval(+(" + data + ")+)";
    if(json['msg'] != "") {

        alert(data); 
    }

    old_msg_id = json['old_msg_id']; 
    setTimeout('waitForMsg()',1000);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown){
    alert("error: " + textStatus + " (" + errorThrown + ")");
    setTimeout('waitForMsg()',15000);
    }


    });  // end ajax
} //end waitformsg


$(document).ready(function(){ 
 waitForMsg();
});

</script>
</head>
<body>
<div id="chat">
</div>
</body>
</html>
<----------------------------------------------------------->

poll.php
<----------------------------------------------------------->
<?php
include("db.php");

$old_totalsum = $_GET['old_totalsum']; 


$result = mysql_query("SELECT sum( r ) as totalsum FROM (SELECT colA, colB, COUNT( * ) AS r FROM product GROUP BY productid ) AS t");
while($row = mysql_fetch_array($result))
{
    $last_sum = $row['totalsum']; 
}



while($last_sum < $old_totalsum || $last_sum > $old_totalsum)


    while($last_sum <= $old_totalsum)
    {
    usleep(1000);
    clearstatcache();
    $result = mysql_query("SELECT sum( r ) as totalsum FROM (SELECT colA, colB, COUNT( * ) AS r FROM product GROUP BY productid ) AS t");
        while($row = mysql_fetch_array($result))
        {
         $last_sum = $row['totalsum'];
            //echo $last_sum;
            //return;
        }

    }





$response = array();
$response['msg'] = 'new';
$response['old_msg_id'] = $last_sum;
echo json_encode($response);
?>
<------------------------------------------->
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:30:10+00:00Added an answer on June 12, 2026 at 4:30 pm

    I expected logic like this:

    $latest_sum= -1;
    // Loop until sum from DB is different from what was passed  in
    while( $latest_sum == -1 || $latest_sum!= $old_sum ){
      $result = mysql_query("SELECT sum( r ) as totalsum FROM (SELECT colA, colB, COUNT( * ) AS r FROM product GROUP BY productid ) AS t");
       while($row = mysql_fetch_array($result))
       {
          $lastest_sum = $row['totalsum']; 
       }
    }
    
    $response = array();
    $response['msg'] = 'new';
    $response['old_msg_id'] = $latest_sum;
    echo json_encode($response);
    

    If it takes a long time to change, then Apache (or whatever is running PHP) will likely cancel the request. This is also going to run lots of queries on the db which is expensive.

    So it would be better to have your loop in the javascript. It would call a function that would just return the latest sum from the database. The javascript should wait a second or more inbetween each request.

    If performance is still too hard on the database, then the php should somehow cache the value every minute and return the cached value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to migrate data from SQL Server to mysql, and i'd like
I tried the following code, but it doesn't work. How should it be modified?
tried uncommenting pam_limits.so from the pam.d directory but no luck. Basic PAM seems to
Tried examples from 'php.net' but don't understand what's the problem. Any suggestions? <?php $_SESSION['test']
This looks like a long question because of all the context. There are 2
I'm a long-time eclipse user looking to use netbeans now that I've tried netbeans
I have modified the code in this question ,according to the answers there, in
I've been struggling with the below piece of code for far too long, trying
I am trying to modify the NotePad sample code from the Android developer resources.
I would like to open a small video file and map every frames in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.