doushua7737 2018-07-20 08:53
浏览 72


How can I initialize the lastID in jquery only first time with the value 0 because var lastID = 0; does not work.

I have this ajax script in index.php

        $(document).ready(function () {
            var lastID = 0;
            function getData() {
                    type: 'GET',
                    url: 'data.php',
                    data: {lastID: lastID},
                    dataType: 'json',
                    success: function (data) {
                        lastID = data[0].id;
                            $.each(data, function (i, item) {
                                var $tr = $('<tr>').append(



            setInterval(function () {
            }, 10000);  // it will refresh your data every 10 seconds


This is the url: 'data.php' :

$sql = "select * from oders where id > ".$lastID." ORDER BY id DESC"; ... echo json_encode($dataArray);

With this query I get 0 results, and in console (console.log(lastID);) I have no data.

If I change the query like this :

$sql = "select * from oders where id > 0 ORDER BY id DESC";

In console.log(lastID); I get the correct last id. And in html I get the correct data and every 10 seconds it keeps adding same results over and over again.

I can't figure out how to initialize lastID first time as 0 (or the last ID in the database), and on every 10 seconds refresh, take the last ID from the ajax success data.

  • 写回答

1条回答 默认 最新

  • douzhang1955 2018-07-20 09:01

    Where does your $lastID variable come from, how is it defined?

    It looks like you are not getting the correct value from the $_GET array.

    What you should do to both solve that problem and the sql injection problem you have, is switch to a prepared statement:

    $sql = "select * from oders where id > ? ORDER BY id DESC";

    And bind $_GET['lastID'] to the placeholder before / during the execution of your query (depending on whether you are using mysqli or PDO).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥30 电脑误删了手机的照片怎么恢复?
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办
  • ¥15 有人懂怎么做大模型的客服系统吗?卡住了卡住了
  • ¥20 firefly-rk3399上启动卡住了