duan5362 2014-09-13 04:24
浏览 28
已采纳

如何在MySQL中插入Ajax代码来获取新行?

I have a simple chat service. I store messages, login and time etc. in Mysql database. The chat messages are displayed below with the help of Ajax & PHP

<div id="messages"> </div><!--- chats are displayed here -->

I have following Ajax code which fetches datas from Mysql in every 2 seconds. Ofcourse, everyone recommends not to do this way. It might effect server performances negatively. And its unnecessary.

$(document).ready( function() {

     var destination_hashed = $("#destination_hashed").val();

     var interval = setInterval( function() {

            $.ajax ({
                     type: "POST",
                     url: "chat.php",
                     data: { destination_hashed1: destination_hashed },

                     success: function(data) {
                            $("#messages").html(data);
                     }
             });
     }, 2000);
});

In nutshell, I have two chat clients A & B. When A sends message to B, there are new rows inserted in Mysql.

So, how can I write Ajax & PHP code to fetch only when there are new rows. Rather than fetching data from Mysql every 2 seconds whether new rows inserted or not

  • 写回答

1条回答 默认 最新

  • doulu6314 2014-09-13 04:55
    关注

    Recently I have worked on a chat module of this kind and I can say some correction in you code

    First of all don't use setInterval in the way you are using ,

    Why

    because in your particular code the request is send to the server every 2 second , so if the network is slow your code will have the tendency to send to much request to the server and coordination between those request would be difficult.

    So what you do is

    function getNewMessage(){
    
                $.ajax ({
                         type: "POST",
                         url: "chat.php",
                         data: { destination_hashed1: destination_hashed },
    
                         success: function(data) {
                                $("#messages").html(data);
                         },
                         complete : function(){  // this will be called even if our request fail, success etc
                             setTimeout(function(){   // send request after 2 second from the complition of the previous request
                                 getNewMessage();       
                             },2000);
                         }
                 });
    

    }

    There are two approach in parsing the chat result from the ajax call

    a) get the html from the ajax call (this would be slow and inefficient in case you want to extend your module in the future)

    You can simpley get the html content from the ajax call on succes parameter you can append that to the table

     For example : -
    
       if the response from the server on new message is 
    
         <div class="chat"> 
           <lable>John</lable>
             Hi there !!!
        </div>
    

    Then parsing the content will be something like this

                 success: function(data) {
                        // $("#messages").html(data);   //this will change the content in every request
                        $("#messages").append(data);   // this will append the new conent
                 },
    

    b) Well the other approch would be to get data in json format(a lot better approch )

    For example : -

       if the response from the server on new message is 
    
         { 
           username : 'john',
           message  : 'Hi there !!!'
         }
    

    Then parsing the content will be something like this

                 success: function(data) {
                        // $("#messages").html(data);  this will change the content in every request
                        $("#messages").append('<div class="chat">'+
                                                '<lable>'+data.username+'</lable>'+
                                                  data.message +
                                              '</div>');    this will append the new conent
                 },  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算