代码如下
while(true){ $getitem = mysql_query("select * from bulletin order by id desc limit 1"); $item = mysql_fetch_array($getitem); echo json_encode($item,JSON_UNESCAPED_UNICODE); ob_flush(); flush(); ob_clean(); //不太懂ob_clean的作用 mysql_data_seek($getitem,0); sleep(1); }
php缓存区通过ob_flush和flush可以把缓存区的内容输出给浏览器,而ob_clean的作用是清空缓存区,所以预想的结果是每次只输出最后一条数据。但是实际上之前的输出并没有清空,请问怎么实现我的需求呢?
Usage of the following three functions
ob_start() opens a buffer on the server to save all output. So anytime echo is used, the output will be added to the buffer until the program ends or is terminated using ob_flush(). Then the content of the buffer in the server will be sent to the browser, which will be parsed and displayed by the browser.
The function ob_end_clean will clear the contents of the buffer and close the buffer, but will not output the content.
At this time, a function ob_get_contents() must be used in front of ob_end_clean() to obtain the contents of the buffer.
In this case, the content can be saved to a variable before executing ob_end_clean(), and then the variable can be operated after ob_end_clean().