简体   繁体   English

使用PHP实时从MySQL中获取数据?

[英]Grabbing data from MySQL using PHP realtime?

I am wondering how I would get data from a MySQL database and display it in real time using PHP. 我想知道如何从MySQL数据库获取数据并使用PHP实时显示它。 Without having to refresh the page. 无需刷新页面。 Thanks! 谢谢!

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

You can use Socket.Io for more Speed ,efficiency and performance for Your Server 您可以使用Socket.Io为您的服务器提供更高的速度,效率和性能

http://socket.io/ http://socket.io/

But you need Node.Js for that 但是你需要Node.Js

You will have to use javascript. 你将不得不使用JavaScript。 You can use setInterval(function, n) to fire your update calls every n milliseconds, and a library like jQuery to handle the ajax call and updating your page. 您可以使用setInterval(function, n)n毫秒触发一次更新调用,并使用像jQuery这样的库来处理ajax调用并更新页面。

Download jQuery or link to a CDN hosted version of jQuery in your page. 在您的页面中下载jQuery或链接到CDN托管的jQuery版本。 Then put something like this on your page: 然后在您的页面上放置这样的内容:

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.

Read the jquery docs under 'ajax' to understand the jQuery.ajax() call, and read about 'selection' and 'manipulation' if you don't understand how to update the html page with the results from your ajax call. 阅读'ajax'下的jquery文档以了解jQuery.ajax()调用,如果您不了解如何使用ajax调用的结果更新html页面,请阅读'selection'和'manipulation'。

The other way to continuously update your page is to use a persistent connection, like web sockets (not currently supported across all the common browser platforms) or a comet-style server-push setup. 另一种不断更新页面的方法是使用持久连接,例如Web套接字(目前不支持所有常见的浏览器平台)或彗星式服务器推送设置。 Try googling comet and/or web sockets for more info, but I think the above method is going to be much easier to implement. 尝试使用Google彗星和/或网络套接字的详细信息,但我觉得上面的方法将是容易实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM