An IFRAME implementation of long polling, through PHP query database and JS Update page content of the program, the problem is not every MySQL insert message can be displayed to the page, ask to help analyze where there is a problem

Source: Internet
Author: User
Tags mysql insert
This post was last edited by stneo1990 on 2013-08-08 11:45:06

First of all, this is a real-time customer service consulting system, the main purpose is to simulate the foreground user sent messages (through the manual insert into the MySQL database implementation) by the server real-time push to the front desk customer Service page, the approximate structure is as follows:
1, in the foreground kefu.html page has an IFRAME, this IFRAME request background query.php Query program (IFRAME hidden on the page)
2, query.php through a while (true) dead loop and set Set_time_limit (0), and then, in the Loop continuous SQL query, the user sent an unread message query out, and by the output JS code, operation of the parent page <textarea></textarea>Output the query results to the page
3, I manually insert some data, to simulate the user's message submission, the default message is unread state, when the query.php read, will change its read state

Now the problem is: when I open the page (or close the page and then reopen the program), the most initially inserted data cannot be displayed on the page, However, the status of these messages has been changed to read query.php (that is, query.php has queried this record and sent the JS code to the front end), and when I continue to insert a number of bars, the front end will start to display the record, and the previous insert record is marked as read, but it is not displayed to the foreground ke fu.html page, after inserting many bars, or waiting for a period of time to return to normal

The whole program is not a problem, because after the program is normal can be a bar display, insert a front desk to display one (but occasionally lost one in the middle)

I guess the root cause of the problem may be two points:
1. PHP scripts are not ended
2, JS may have a cache resulting in although the data received, but not updated

But I am not sure of the above two points, I think the problem may not be so simple, it should be violated or not pay attention to a mechanism caused by.

The specific code below, also ask you to help to see where the problem is, can also be tested locally, thank you.

Test method:
Insert data in the database manually, and then observe the foreground kefu.html page to receive and update the message in a timely manner

Database Build Table:
CREATE TABLE msg (
ID Int (one) not NULL auto_increment,
POS varchar (+) not NULL default ' ',
Rec varchar (+) not NULL default ' ',
Content varchar (+) not NULL default ' ',
Isread ' int (1) Not NULL default ' 0 ',
PRIMARY KEY (ID)
);

Insert Data Example:
INSERT into MSG (rec,pos,content,isread) VALUES (' admin ', ' zhangsan ', ' Hello ', 0);


Reply to discussion (solution)

Front desk kefu.html Code:

 
        Online Customer service System    
 
                  

Online customer service System--customer service side

Reply:

Back-end Query query.php code:

 
  '; Ob_flush (); flush (); $i = 0;require ' conn.php ';//Connect database File//continuously iterate query message while (TRUE) {//Query receiver is admin, and is unread message $sql = ' SELECT * From msg where rec = "admin" and isread = 0 limit 1 '; $result = $conn, query ($sql);//extract results $rs = $result, fetch_a SSOC (); if (!empty ($rs)) {//updates this message to a read state $sql = ' Update msg set isread = 1 WHERE id = ' '. $rs [' id ']. ' "Limit 1 '; $conn-Que Ry ($sql);//Convert to JSON format ready to send to Js$rs = Json_encode ($rs),//Call the parent form's Comet () function via Paren, and transfer the converted JSON data past, Comet () function can find echo ' in the kefu.html Code;} Ob_flush (); flush ();//prevent too many queries, set every 1 seconds to query sleep (1);}

Database Connection conn.php Code:

 
  Query (' Set names UTF8 ');

The recommendation is to use the Ajax polling method, this kind of background query script does not actively end the script execution, there may be some problems.

I'm testing only one of the back shows.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.