0

我觉得这是一个比任何事情都更逻辑的问题。数据库具有通过源引用保存的图片和标签的布尔值,例如 isLandscape=1。我已经建立了一个系统来根据所询问的类型遍历结果页面。以下是我所面临的一个例子。我从第 0 页 -> 第 22 页只看到相同的 12 张图片。然后我开始看到新的。我想我只是忽略了这个错误,因为直到现在我才注意到它。我注意到的一件事是 page22*12pictures = 264,这与看到的第一个新图片 id 相同。您可以在此处看到错误(只需将 p 更改为不同的页面)。

<?php
$pictureid   = -1;
$startpage   = 0;
$viewsection = -1;
$uid         = -1;  //user id

$amntperrow  = 4; //how many pictures per row, must correlate with doThumb()'s switch case amounts
$maxrows     = 3;    //how many rows of pictures to drop

if(isset($_GET['pid']) && is_int(intval($_GET['pid']))) $pictureid   = clean($_GET['pid']);
if(isset($_GET['sec']) && is_int(intval($_GET['sec']))) $viewsection = clean($_GET['sec']);
if(isset($_GET['p'])   && is_int(intval($_GET['p'])))   $startpage   = clean($_GET['p']);

$result = generateResult(array("isFlowers"), $startpage);
//**snip** -- drawing thumbnails would happen here

function generateResult($types, $page) {
    global $amntperrow;
    global $maxrows;

    $sqlWheres = "";
    $idAmnt = ($amntperrow*$maxrows)*$page;

    if(isset($types) && !empty($types)) {
        if(count($types) >= 1) {
            for($i = 0; $i<count($types); $i++) {
                $sqlWheres .= $types[$i] . "='1'";
                if($i < count($types)-1) $sqlWheres .= " AND ";
            }
        }
    }        

    $result = "SELECT * FROM pictures WHERE ";
    if(!empty($sqlWheres)) $result .= $sqlWheres . " AND " ;
    $result .= " private='0' AND id >='" . $idAmnt . "' LIMIT " . ($amntperrow*$maxrows);

    return $result;
}
?>

这似乎是一个我忽略的明显错误。谢谢您的帮助。

4

1 回答 1

1

这两个查询有什么区别?

SELECT *
FROM pictures
WHERE private = '0' AND id >= '24'
LIMIT 12;

SELECT *
FROM pictures
WHERE private = '0' AND id >= '36'
LIMIT 12;

答:可能根本没有区别。数据库引擎可以在任何一种情况下决定它想要返回ids 100 到 111 的图片 - 结果集满足任一查询的所有条件。

请尝试这样的查询:

"SELECT *
FROM pictures
WHERE private = '0'
ORDER BY id
LIMIT " . $idAmnt . ", " . ($amntperrow * $maxrows)

ORDER BY id真的是关键。对数据库结果进行分页通常使用 和 的组合来ORDER BY完成LIMIT

于 2012-05-23T18:28:44.627 回答