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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题