PHP:数据库扩展-当前行和最新行

8mmmxcuj  于 2023-02-15  发布在  PHP
关注(0)|答案(1)|浏览(120)

在我的JavaScript长轮询脚本中,我调用msgsrv.php来回显还没有显示在屏幕上的num_rows

<script type="text/javascript" charset="utf-8">
function addmsg(type, msg){
    /* Simple helper to add a div.
    type is the name of a CSS class (old/new/error).
    msg is the contents of the div */
    $("#notiWrap.notiZero").html(
        "<div id='notiZero "+ type +"'>"+ msg +"</div>"
    );
}

function waitForMsg(){
    /* This requests the url "msgsrv.php"
    When it complete (or errors)*/
    $.ajax({
        type: "GET",
        url: "http://mediahood.net/widgets/txtr/inc/msgsrv.php",

        async: true, /* If set to non-async, browser shows page as "Loading.."*/
        cache: false,
        timeout:50000, /* Timeout in ms */

        success: function(data){ /* called when request to barge.php completes */
            addmsg("notiMsg", data); /* Add response to a .msg div (with the "new" class)*/
            setTimeout(
                waitForMsg, /* Request next message */
                1000 /* ..after 1 seconds */
                
            );
        },
    });
};
$(document).ready(function(){
    waitForMsg(); /* Start the inital request */
    
});
</script>

但是,msgsrv.php知道数据库中实际的最新行,但不知道页面上加载的最新行的id。
$num_rows工程;$loaded_rows失败。$loaded_rows失败,因为当JS调用msgsrv.php时,它重置为数据库的实际num_rows。
我需要PHP读取页面上的$loaded_rows,而不是数据库中的$loaded_rows,因为我已经知道如何查询最新的行。
下面是我的msgsrv.php:

<?php
/* Send a string after a random number of seconds (2-10) */
sleep(2);

include('/home/alimix/public_html/include/conn.php');

/* Variables */
$nr = mysql_query("SELECT * FROM txtr");
while (mysql_fetch_array($nr))
{$nowrecent =  mysql_num_rows($nr);}

$mr = mysql_query("SELECT * FROM txtr ");
for ($i=0; $i < 1; $i++)
{$mostrecent = "$nowrecent";}
/* $mostrecent shouldn't equal $nowrecent if new rows aren't loaded*/
    $minus = $nowrecent - $mostrecent;

        if ($minus > 0) {
            //echo $nowrecent;
            echo $minus;
            //echo $mostrecent;
        }
        elseif($minus < 1) {
            echo "nr".$nowrecent."";
            echo " ";
            echo "mr".$mostrecent."";
        }
?>
lyr7nygr

lyr7nygr1#

而不是喂养这个:

url: "http://mediahood.net/widgets/txtr/inc/msgsrv.php",

喂这个:

url: "http://mediahood.net/widgets/txtr/inc/msgsrv.php",
    data: {
      lastID: mylastID
    },

每次获取一行数据时,将mylastID设置为新的最新ID(并确保mylastID在 AJAX 调用的作用域中),这样就可以使用$_GET ['lastID']恢复该值了!

相关问题