如何自动增加 mysql 限制数


How can I auto increment mysql LIMIT number?

所以我用这个 msql 查询加载了 3 条记录:

$query = "SELECT * FROM adatok ORDER BY `id` DESC LIMIT 3 "

我为用户创建了一个加载更多按钮,可以使用此查询加载其他 3 条记录:

$query = "SELECT * FROM adatok ORDER BY `id` DESC LIMIT 4,3 

然后问题来了。

当用户再次单击"加载更多"按钮时,这将获得与收到的记录相同的记录。那么如何自动递增限制数呢?演示页面:

http://neocsatblog.mblx.hu/addvideos/type.html按 crtl+i 打开框。
更新:
ID 会自动递增。
感谢您对@spencer7593的改进。
我的桌子结构看起来像这样。

完整的php代码,如果你点击更多按钮会发生什么:

<?php
$connection = mysql_connect('localhost', 'neocsat_videos', 'password'); //The Blank string is the password
mysql_select_db('neocsat_videos');
mysql_query("SET CHARACTER SET utf8 ");
$page = 1
$query = "SELECT * FROM adatok ORDER BY `data_reg` DESC LIMIT 3,4 ";
$result = mysql_query($query);

while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
echo '<div class="video-header">';
if (!function_exists('echoOnce')) { 
$runOnce = false;
function echoOnce()
{
    global $runOnce;
    if(!$runOnce)
    {
        $runOnce = true;
        return "<img class='close2' src='/kep/icon_24x24_close_highlight.png'>";
    }
}
}
$datatime=$row['date_reg'];
$img =  '<img title="' . $datatime . '" src="time.png" class="time_icon" >';
echo echoOnce();
echo '<p>';
echo   $row['name'];
echo '</p>'; 
echo $img;
echo '</div>';
echo '<div class="result">';
echo ' <iframe class="video" allowfullscreen style="overflow-x: hidden; overflow-y: hidden;" width="658px" height="569"  frameborder="0" src="'.$row['url'].'"></iframe>' ;
echo '</div>';
echo '<div class="leiras">';
echo '<p>';
echo   $row['leiras'] ;  //$row['index'] the index here is a field name
echo '</p>';
echo '</div>';
echo '<div class="clear">';
echo '</div>';
?>
<a class='load'><div  class='more'>További videók betöltése</div></a>
<?php
$connection = mysql_connect('localhost', 'neocsat_videos', 'zP77XRavaXMA'); //The Blank string is the password
mysql_select_db('neocsat_videos');
mysql_query("SET CHARACTER SET utf8 ");
$page = 1
$query = "SELECT * FROM adatok ORDER BY `data_reg` DESC LIMIT 3,4 ";
$result = mysql_query($query);

while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
echo '<div class="video-header">';
if (!function_exists('echoOnce')) { 
$runOnce = false;
function echoOnce()
{
    global $runOnce;
    if(!$runOnce)
    {
        $runOnce = true;
        return "<img class='close2' src='/kep/icon_24x24_close_highlight.png'>";
    }
}
}
$datatime=$row['date_reg'];
$img =  '<img title="' . $datatime . '" src="time.png" class="time_icon" >';
echo echoOnce();
echo '<p>';
echo   $row['name'];
echo '</p>'; 
echo $img;
echo '</div>';
echo '<div class="result">';
echo ' <iframe class="video" allowfullscreen style="overflow-x: hidden; overflow-y: hidden;" width="658px" height="569"  frameborder="0" src="'.$row['url'].'"></iframe>' ;
echo '</div>';
echo '<div class="leiras">';
echo '<p>';
echo   $row['leiras'] ;  //$row['index'] the index here is a field name
echo '</p>';
echo '</div>';
echo '<div class="clear">';
echo '</div>';
?>
<a class='load'><div  class='more'>További videók betöltése</div></a>
<?php
}
mysql_close(); //Make sure to close out the database connection
?>
<meta charset="utf-8">
<link href='http://fonts.googleapis.com/css?family=Nunito:700' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Lobster' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="style2.css">
<style>
#video-body,.video-body{
    background-color:white;
    width:780px;
    position: fixed !important;
    top: 100px;
    right: -799px;
    border-radius: 5px;
    padding-left: 13px;
    padding-bottom: 30px;
    max-height: 453px;
    overflow-x: hidden;
    }
    #video-header,.video-header{
    border-bottom: 6px solid gray;
    margin-left: 73px;
    width: 628px;
    }
    #result,.result{
    margin-left: 62px;
    }
    #video-header p,.video-header p{
    margin-left: 158px;
    font-family: 'Nunito', sans-serif;
    font-size: 22px;
    font-weight: bold;
    padding-top: 12px;
    margin-bottom: 11px;
    }
    #clear,.clear{
    border-bottom: 6px solid gray;
    width: 625px;
    margin: 50px 82px;
    }
    #leiras, .leiras{
    margin: -274px 139px 28px;
    font-family: 'Nunito', sans-serif;
    font-size: 13px;
    font-weight: bold;
    word-wrap: break-word;
    background-color: rgba(0, 0, 0, 0.8);
    border-radius: 5px;
    min-height: 29px;
    text-align: center;
    padding: 3px;
    }
    .leiras p{
    color:white;
    }
</style>
<script>
   $(".close2").click(function(){
          $(".video-body").fadeOut();
           $( "iframe" ).remove();
           $( "div" ).remove();
           $( "p" ).remove();
          });
$(".video-body").on('click', '.load', function() {
jQuery.ajaxSetup({ async: false }); //if order matters
                  $.get("next.php", '', function (data) { $("#video-body").append(data); });
});
$( ".time_icon" ).tooltip({
      show: null,
      position: {
        my: "left top",
        at: "left bottom"
      },
      open: function( event, ui ) {
        ui.tooltip.animate({ top: ui.tooltip.position().top + 10 }, "fast" );
      }
    });
</script>
<script>
// The plugin code
(function($){
    $.fn.urlToLink = function(options) {
        var options = $.extend({}, $.fn.urlToLink.defaults, options); 
        return this.each(function(){
            var element = $(this),
                expression = /('b(https?|ftp|file|https|http):'/'/[-A-Z0-9+&@#'/%?=~_|!:,.;]*[-A-Z0-9+&@#'/%=~_|])/ig;
            // The magic
            return element.html( element.text().replace(expression, "<a class=lightview href='$1' target='"+options.target+"'>$1</a>") );
        });
    }
    /**
     * Default configuration
     */
    $.fn.urlToLink.defaults = {
        target : '_self'         // Link target
    }
})(jQuery)
// The call
$('p').urlToLink();
</script>

Javascript:

$(".video-body").on('click', '.load', function() {
jQuery.ajaxSetup({ async: false }); //if order matters
               $( "#video-body" ).load( "next.php" );

});
$limit = 2; //how many items to show per page
$page = $_GET['page'];
if($page) 
    $start = ($page - 1) * $limit;          //first item to display on this page
else
    $start = 0;                             //if no page var is given, set start to 0
$sql = "SELECT column_name FROM tbl_name LIMIT $start, $limit";

$query = "SELECT * FROM adatok ORDER BY id DESC LIMIT ".($page*3 ).",3";其中$page是按下更多按钮的次数。
第一次单击$page = 1 ,第二次单击$page = 2,依此类推

假设id是唯一的,正常的模式是保存上一个查询检索到的最后一个id值:

那么"下一个"查询的形式将是:

SELECT ... FROM adatok WHERE id < :last_id ORDER BY id DESC LIMIT 3

:last_id占位符提供上次检索到的id值。

例如,上一个查询返回 id 值 214、212、211。 我们在页面上"保存"211 的值。 当用户单击"更多数据"时,我们获取该值 211,并在"下一行"查询中提供它。

SELECT ... FROM adatok WHERE id < 211 ORDER BY id DESC LIMIT 3
                                  ^^^

此查询保证返回上一个查询检索到的 id 值。如果此查询的执行返回 id 值 210、209、208... 我们保存上次检索到的 ID 值 208。后续的"下一个"查询将使用该保存的值...

SELECT ... FROM adatok WHERE id < 208 ORDER BY id DESC LIMIT 3
                                  ^^^

等等。


OP "next" 查询将跳过第四行;LIMIT 的第一个参数应该是之前检索到的行数...限制 3,3。 (在 OP 查询中使用 LIMIT 4,3 实际上将"跳过"第四行。

对于接下来的集合:

... LIMIT 6,3
... LIMIT 9,3

此方法的一个问题是,如果对表有任何具有较大id值的插入,由于查询是按id值降序排序的,因此后续的"下一个"查询可能会返回以前检索到的行。如果删除具有较高 id 值的行,则此表单可能会"跳过"某些id值。

使用第一种形式的查询,在我的答案顶部,结果不受插入/删除具有较高id值的行的影响。