weixin_33709590 2013-03-15 19:31 采纳率: 0%
浏览 58

如何实时显示数据库

I has some question about live database.

that mean when database change that value, the page will show directly something like Live Chat.

    Language : Jquery + PHP

Example:

    MySQL Database:
    Table : user
    column : username,Status,Date,TodayOnline
    Data : [Kenny, Online,15/03/2013,0],[David, Offline,16/03/2013,1]

Action:

    When David going to online, it will show at page directly by using replace the "Kenny is now Online" with "David is now online"
    and show Today online time mean how many time he is only today.

Screen Show:

    David Is Now Online (Today Online : 1)

Wish you guy understand what i mean, i dont know how to do it by ajax,or who can tell me how to make ajax run every second or any other good idea to show it instant/live?

Thanks in Advance.

UPDATE

Here is my code JS

$.ajax({
        type: "post",
        url: "/checkOnline.php",
        cache: false,
        data:{ date: $("#date").val(),time: $("#time").val()},
        success: function(responds){
            var obj = JSON.parse(responds);
                try{
                    ('#online').text(responds+" Is Now Online");
                }catch(e){      
                    alert('Exception while request..');
                }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
                    alert(XMLHttpRequest.status);
                    alert(XMLHttpRequest.readyState);
                    alert(textStatus);
        }
    });

Here and my index code i adding hiding input box for get current date time.

        <input name="date" id="date" value="<?= date('Y-m-d') ?>" style="display:none"/>
        <input name="time" id="time" value="<?= date('H:i:s') ?>" style="display:none"/>

and this is my checkOnline.php

<?php
$query = "Select username FROM user WHERE status='online'";
$result = mysql_query($query);

echo json_encode($result)
?>

i run the ajax when someone load the page using jquery but i know this is a bad idea, but i dont know how to make it live.

Thanks

  • 写回答

2条回答 默认 最新

  • weixin_33736832 2013-03-15 19:39
    关注

    basically, in order to give the illusion of persistence and real time updates, you'll need to do some serious development.

    but there are areas of javascript that are really getting into real time view rendering as data updates: do a google search for "backbone" and "backbone marionette" and "pubsub javascript" and "comet"

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog