使用PHP实时从MySQL抓取数据?

使用PHP实时从MySQL抓取数据?,php,mysql,real-time,Php,Mysql,Real Time,我想知道如何从MySQL数据库中获取数据,并使用PHP实时显示数据。无需刷新页面。谢谢 使用AJAX(我建议使用jQuery库),并让您的AJAX脚本(用PHP编写)查询MySQL数据库。您必须使用javascript。您可以使用setInterval(function,n)每隔n毫秒触发一次更新调用,并使用类似jQuery的库来处理ajax调用和更新页面 在您的页面中下载或链接到托管CDN的jQuery版本。然后在您的页面上放置类似的内容: setInterval(function(){

我想知道如何从MySQL数据库中获取数据,并使用PHP实时显示数据。无需刷新页面。谢谢

使用AJAX(我建议使用jQuery库),并让您的AJAX脚本(用PHP编写)查询MySQL数据库。

您必须使用javascript。您可以使用
setInterval(function,n)
每隔
n
毫秒触发一次更新调用,并使用类似jQuery的库来处理ajax调用和更新页面

在您的页面中下载或链接到托管CDN的jQuery版本。然后在您的页面上放置类似的内容:

setInterval(function(){
    // inside here, set any data that you need to send to the server
    var some_serialized_data = jQuery('form.my_form').serialize();

    // then fire off an ajax call
    jQuery.ajax({
        url: '/yourPhpScriptForUpdatingData.php',
        success: function(response){
            // put some javascript here to do something with the 
            // data that is returned with a successful ajax response,
            // as available in the 'response' param available, 
            // inside this function, for example:
            $('#my_html_element').html(response);
        },
        data: some_serialized_data
    });
}, 1000); 
// the '1000' above is the number of milliseconds to wait before running 
// the callback again: thus this script will call your server and update 
// the page every second.
阅读“ajax”下的链接以了解jQuery.ajax()调用,如果不了解如何使用ajax调用的结果更新html页面,请阅读关于“选择”和“操作”的内容


另一种持续更新页面的方法是使用持久连接,如web套接字(目前不支持所有通用浏览器平台)或comet样式的服务器推送设置。尝试用谷歌搜索comet和/或web Socket以获取更多信息,但我认为上述方法将更容易实现。

您可以使用Socket.Io来提高服务器的速度、效率和性能


但是您需要Node.Js来实现这一点

这会导致每秒一个mysql连接。关闭连接似乎不起作用。我们如何防止连接最大化?