首页 文章

如何检查数据库中是否有添加的记录

提问于
浏览
0

我这里有我的index.php . 它已经包含了布局和里面的php代码 . 我只是想知道数据库上是否有新记录 . 根据我的研究,它需要更新 . 我尝试了javascript自动刷新,整个页面现在刷新,所以如果有新记录,表将自动显示新记录 . 但现在我的问题是如何知道是否有变化 . 我能够通过刷新显示数据库是否有更新,但是如果有更改,我无法通知 . 我只想在有新记录时发出通知 . 所以现在我想知道如何检查数据库上是否有新记录 . 这是我的代码 .

<div class="panel panel-default">
    <div class ="panel-body">


    <!-- TABLE OF REPORTS -->
    <div align = "center" style = " font-size: 9000px">
        <table class="table table-striped" name = "report">
            <thead>
                <tr align = "center"class = "active" >

                    <td class  = "theads" >Id</td>
                    <td class  = "theads" >Message</td>
                </tr>
            </thead>

            <!--Start of php code -->
            <?php   
            $page=1;//Default page
            $limit=10;//Records per page
            $start=0;//starts displaying records from 0
            if(isset($_GET['page']) && $_GET['page']!='')
            {
                $page=$_GET['page'];
            }
                $start=($page-1)*$limit;

                //Get total records (you can also use MySQL COUNT function to count records)
                $query=mysql_query("select report_id from reports");
                $rows=mysql_num_rows($query);

                $query=mysql_query("select * from reports order by datetime DESC LIMIT $start, $limit");


                if(mysql_num_rows($query)>0)
                {
                    while($row=mysql_fetch_array($query,1))
                    {
            ?>

            <tbody>
                <tr class = 'active'>
                <td align = 'center'><?php echo $row['report_id'] . "<br>";?></td>
                <td align = 'center'><?php echo $row['message'] . "<br>";?></td>
                </tr>
            </tbody>

                <?php
                    }
                }
            ?>
        </table>

    </div>

    </div>
</div>

1 回答

  • 0

    如果要在客户端执行此通知系统,则可以创建具有最新创建日期的cookie,并检查任何创建日期是否大于cookie的日期,然后计算具有更大日期然后cookie值的行 .

    如果您想在服务器端执行此操作,则必须将上次看到的创建日期保存在数据库表中的某些位置并执行上述过程 .

    $query=mysql_query("select * from reports order by datetime DESC LIMIT $start, $limit");
    if(isset($_SESSION['chk']))
    {
        $sql=mysql_query("select count(*) as new_record from reports where datetime>$_SESSION['chk']");
        $count=mysql_fetch_array($sql);
        $new_record=$count['new_record']; // new records
    }
    
    if(mysql_num_rows($query)>0){$i=0;
    while($row=mysql_fetch_array($query,1)){if($i==0){$_SESSION['chk']=$row['datetime'];$i++;}
    ?>
    

相关问题