简体   繁体   English

PHP从MySQLI获取数据[删除此]

[英]PHP Get Data From MySQLI [delete this]

Hello I'm trying to get live data from sql table, example if one rows is inserted show me with data like notification system 您好我正在尝试从sql表中获取实时数据,例如,如果插入了一行,则向我显示通知系统之类的数据

with my code when i insert new row not fetch me... !? 当我插入新行时使用我的代码无法获取我...!?

index.php: index.php:

 <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script>
        $(document).ready(function() {
            setTimeout(
            function() {updateChat();},
            1000);

            function updateChat() {
                $.get("read.php", function(data)
                {
                  $("#Show_Data").html(data);
                });
            }
        });
    </script>
    <div id="Show_Data"></div>

read.php read.php

include('config.php');

$sql_query = mysqli_query($connection, "SELECT * FROM shout_box where id > 15 ORDER BY id");

while($fetch_data = mysqli_fetch_array($sql_query, MYSQLI_ASSOC)) {
    echo 'ID: ' .$fetch_data['id']. ' Text: '. $fetch_data['text']. '</br>';
}

mysqli_close($connection);

在此处输入图片说明

Use setInterval 使用setInterval

var myCall = setInterval(updateChat, 1000);

//for to stop calling
function myStopFunction() {
   clearInterval(myCall);
}

The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds). setInterval()方法以指定的时间间隔(以毫秒为单位)调用函数或计算表达式。

The setInterval() method will continue calling the function until clearInterval() is called, or the window is closed. setInterval()方法将继续调用该函数,直到调用clearInterval()或关闭窗口为止。

it's a dirty job but fine. 这是一项肮脏的工作,但很好。 you must not get ALL data from database every 1s. 您一定不能每1秒从数据库中获取所有数据。 there is huge data after a long chat. 长时间聊天后,会有大量数据。 so you can save last row id that you get from db in an attribute. 因此您可以将从db获取的最后一行ID保存在属性中。 then after 1s you must check all rows after that id or after that time (i see u have a date_time for every row). 然后在1s之后,您必须检查该ID之后或该时间之后的所有行(我看到您每行都有date_time)。

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

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