通过MySQL/PHP递增

通过MySQL/PHP递增,php,mysql,pdo,Php,Mysql,Pdo,我正在寻找一种方法,通过一个表中三条记录的步骤来递增和递减并返回它们 假设ID“4”当前处于活动状态。我想获得下3条和前3条记录,ID和类别分别为3.2.1和5.6.7(通过递增和递减) 到目前为止,我已经: $stmt = $db->query("SELECT id, category FROM test"); $stmt->execute(); while ($results = $stmt->fetch(PDO::FETCH_ASSOC)) { $current

我正在寻找一种方法,通过一个表中三条记录的步骤来递增和递减并返回它们

假设ID“4”当前处于活动状态。我想获得下3条和前3条记录,ID和类别分别为3.2.1和5.6.7(通过递增和递减)

到目前为止,我已经:

$stmt = $db->query("SELECT id, category FROM test");
$stmt->execute();

while ($results = $stmt->fetch(PDO::FETCH_ASSOC))
{
    $current = $results['id'];
    $category = $results['category'];
    $next = array(array(
        'slide_no' => $current, 
        'category' => $category
    ));
}

print_r($next);
使用这个,我将返回表中的每一行

我不知道如何通过3步递增和递减记录,并确保类别也会相应递增


非常感谢。

您需要使用所需的6个ID创建一个字符串

所以如果CatID是4岁你会想要

$selectIDs='1,2,3,4,5,6,7'

然后,当您执行SQl时,您将使用
“从测试中选择id,类别,其中id位于(“.$selectIDs.””);

如果我正确理解了您的问题,您希望
对显示的数据进行分页。您需要使用
MySQL LIMIT子句

您需要使用代码生成的值调整查询。请参见下面的示例:

$stmt = $db->query("SELECT id, category FROM test LIMIT ?, 3");
#you'll supply the ? parameter depending on your current start index

您可以使用=>和查询('SELECT id,category FROM test where id=>'。$intDecr.'和
$intId = 4
$intAdd = $intId + 3
$intDecr = $intId -3
$stmt = $db->query('SELECT id, category FROM test where id =>'.  $intDecr.' and <= '.$intAdd. ');