2012-05-23 83 views
0

我覺得這是一個比任何事情都更爲邏輯的問題。數據庫具有通過源參考保存的圖片以及用於標籤的布爾值。 isLandscape = 1。我製作了一個系統,根據查詢的類型遍歷結果頁面。以下是我所面臨的一個例子。我只從第0頁 - >第22頁看到相同的12張照片。然後我開始看到新的。我想我只是忽略了這個bug,因爲直到現在我還沒有注意到它。我注意到的一件事是page22 * 12pictures = 264,它與所看到的第一張新照片ID相同。您可以看到錯誤here(只需將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; 
} 
?> 

這看起來像是我忽略的一個明顯的缺陷。謝謝您的幫助。

回答

1

這兩個查詢有什麼區別?

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

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

答:可能在所有沒有區別。數據庫引擎可以在任何一種情況下決定它想要用id從100到111返回圖片 - 該結果集滿足任一查詢的所有條件。

嘗試這樣的查詢,而不是:

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

ORDER BY id是真正的關鍵。通過數據庫結果分頁通常使用ORDER BYLIMIT的組合來完成。