简体   繁体   English

每隔5秒从MYSQL表读取一次,并在PHP页面上动态显示结果而无需刷新

[英]Reading from a MYSQL table every 5 seconds and dynamically displaying results on a PHP page without refreshing

I'm looking to display data from a table in a mysql database using PHP, however, I want the data to automatically update itself and retrieve current values every 5 seconds.. WITHOUT having to refresh the page. 我正在寻找使用PHP从mysql数据库中的表中显示数据的方法,但是,我希望数据能够自动更新自身并每5秒检索一次当前值。而不必刷新页面。 Is this possible? 这可能吗? Maybe with JQuery/ AJAX? 也许使用JQuery / AJAX? If so, please explain how it can be done / point me to a resource where I can find such information 如果是这样,请说明如何完成/将我指向可以找到此类信息的资源

Thanks 谢谢

If you use window.setInterval() and jQuery's .load() you should be able to do what you want. 如果您使用window.setInterval()和jQuery的.load() ,则应该能够执行所需的操作。 The PHP script should return the HTML that needs to replace the previous one. PHP脚本应返回需要替换前一个HTML的HTML。

Javascript: Javascript:

function refreshData()
{
  // Load the content of "path/to/script.php" into an element with ID "#container".
  $('#container').load('path/to/script.php');
}

// Execute every 5 seconds
window.setInterval(refreshData, 5000);

A really basic example: 一个非常基本的例子:

  function poll(){
       $.ajax({
           type: "GET", 
           url: "your/php/script/",
           success: function(data){
              // do something with data
           }

       });
   };
   setInterval(poll, 5000);

jQuery is a good option. jQuery是一个不错的选择。 Here are the docs for ajax. 这是ajax的文档。

You will want to make this call with setInterval 您将要使用setInterval进行此调用

Something like this might get your started. 这样的事情可能会让您入门。

setIntervla(updateFromDb,5000);

function updateFromDb(){
   $.ajax({
      url: "getUpdates.php",
     success: function(){
     $(this).addClass("done");
     }
   });
};

What you are describing is exactly the type of the AJAX is used for, AJAX allows for asynchronous requests to be made to your server. 您所描述的正是AJAX所使用的类型,AJAX允许向您的服务器发出异步请求。

For learning I would suggest using a framework like Jquery and look into the AJAX api. 为了学习,我建议使用类似Jquery的框架,并研究AJAX api。

Basicly you will need a PHP script that query the database and responds the results the way you want them. 基本上,您将需要一个PHP脚本来查询数据库并以所需的方式响应结果。 A suggestion would be to JSON encode them. 一个建议是对它们进行JSON编码。

In JavaScript on the client you will need to you things like: 在客户端上的JavaScript中,您将需要执行以下操作:

var poll = setInterval(function(){
   $.ajax({
     type:"GET",
     url: "yourpage.php",
     success: function(data){
        //HANDLE DATA
        // use JSON.parse(data); if your JSON encoding your data  
    }
   });
},5000)

Just go to the documentation of jQuery: 只需转到jQuery的文档:

http://api.jquery.com/category/ajax/ http://api.jquery.com/category/ajax/

Use the command "jQuery.get()" or better "jQuery.getJson()" to make a http request to the server. 使用命令“ jQuery.get()”或更好的“ jQuery.getJson()”向服务器发出http请求。 Use JSON to get a better communication between server and client. 使用JSON在服务器和客户端之间获得更好的通信。 Return from server side a json string and convert this on the client to an javascript object. 从服务器端返回一个json字符串,并将其在客户端上转换为javascript对象。 (the function jQuery.getJson already do this for you) so you can easily access the key and values in the data array. (jQuery.getJson函数已经为您完成了此操作),因此您可以轻松访问数据数组中的键和值。

Just an example: 只是一个例子:

SERVER Part with PHP: 使用PHP的服务器部分:

<?

$data = array('key'=>'value');
return json_encode($data, true);

CLIENT Part: 客户部分:

$.getJSON('myurl.php', function(data) {
    // THIS ONE IS CALLED with your PHP data
    alert(data.key);
});
$(function(){
window.setInterval(function(){
    $.post("filename.php",{'field1':field1,'field2':field2,'field3':field3},function(data){
    //callbackfunction(data)
})
},30000);//millisecs

}); });

And have your php file do all your sql 并让您的php文件执行所有SQL

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

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