2012-01-14 4 views
2

내 페이지가 이미지를 표시하고 현재 페이지와 관련된 이전 이미지와 다음 이미지를 표시하려고합니다. 지금은 동일한 쿼리를 3x 실행하고 "where" 문을 =, >, <으로 수정합니다.이 mysql/php 쿼리를 어떻게 최적화 할 수 있습니까?

작동하지만 더 나은 방법이 있어야한다고 생각합니다.

이미지 ID는 1,2,3,4,5가 아닙니다. 1,2,10,20,21 등이 될 수 있습니다. 그러나 훨씬 더 효율적이라면 이것을 바꿀 수 있습니다.

mysql_select_db("database", $conPro); 
$currentid = mysql_real_escape_string($_GET['currentid']); 
$query ="SELECT * FROM database WHERE id ='".$currentid."' LIMIT 1 "; 
$result = mysql_query($query,$conPro) or die(mysql_error()); 
$affected_rows = mysql_num_rows($result);        
if ($affected_rows==1) 
    { 
     $row = mysql_fetch_array($result)or die ('error:' . mysql_error());  
     $current_id = $row['id']; 
     $current_header = $row['title']; 
     $current_description =$row['desc']; 
     $current_image = "http://".$row['img']; 
     $current_url = "http://".$row['id']."/".$db_title."/"; 
     $current_thumb = "http://".$row['cloud']; 
} 

mysql_select_db("database", $conPro); 
$query ="SELECT * FROM database WHERE id <'".$currentid."' ORDER BY id DESC LIMIT 1 "; 
$result = mysql_query($query,$conPro) or die(mysql_error()); 
$affected_rows = mysql_num_rows($result);        
if ($affected_rows==1) 
    { 
    $row = mysql_fetch_array($result)or die ('error:' . mysql_error()); 
     $previous_id = $row['id']; 
     $previous_header = $row['title']; 
     $previous_description =$row['desc']; 
     $previous_image = "http://".$row['img']; 
     $previous_url = "http://".$row['id']."/".$db_title."/"; 
     $previous_thumb = "http://".$row['cloud']; 
    }else{ 
     $previous_none = "true"; //no rows found 
    } 

mysql_select_db("database", $conPro); 
$query ="SELECT * FROM database WHERE id >'".$currentid."' ORDER BY id ASC LIMIT 1 "; 
$result = mysql_query($query,$conPro) or die(mysql_error()); 
$affected_rows = mysql_num_rows($result);        
if ($affected_rows==1) 
    { 
    $row = mysql_fetch_array($result)or die ('error:' . mysql_error()); 
     $next_id = $row['id']; 
     $next_header = $row['title']; 
     $next_description =$row['desc']; 
     $next_image = "http://".$row['img']; 
     $next_url = "http://".$row['id']."/".$db_title."/"; 
     $next_thumb = "http://".$row['cloud']; 
    }else{ 
     $next_none = "true"; //no rows found 
     } 
mysql_close($conPro); 

당신은 select_db마다 할 필요가 없습니다 시간

답변

2

주셔서 감사합니다. 일단 db를 선택하면 다른 것을 선택할 때까지 선택된 상태로 유지됩니다.

당신은 정말 이전/다음 이미지를 얻기 위해 두 개의 쿼리를 수행에서 벗어날 수는 없지만, 당신은 통합 쿼리를 사용하여 그것을 가짜 수 있습니다

(SELECT 'next' AS position, ... 
FROM yourtable 
WHERE (id > $currentid) 
ORDER BY id ASC 
LIMIT 1) 

UNION 

(SELECT 'prev' AS position, ... 
FROM yourtable 
WHERE (id < $currentid) 
ORDER BY id DESC 
LIMIT 1) 

이것은을 포함, 두 개의 행을 반환 pseudofield는 '다음'레코드와 '이전'레코드를 쉽게 식별 할 수있게 해주는 'position'이라는 이름을 갖습니다. [order by] 절이 개별 쿼리에 적용되도록 대괄호가 필요합니다. 없으면, mysql은 union sequence의 마지막 질의에서 order by 절을 가져 와서 전체 union 결과에 적용 할 것이다.

+0

보다 작거나 큰 연산자 대신 평등을 사용할 수 있습니다. 나는 @Shaune이 이것에 대답 할 수 있었다고 생각한다. 그렇다면 ** 주문 ** 및 ** 제한 **을 삭제할 수 있습니다. 또한 yourtable.id에 indexd가 있습니까? –

0

"이전"하나를 먼저 얻고 WHERE id <'".$currentid."' ORDER BY id DESC을 "위에서"두 개 쿼리 할 수 ​​있습니다. SELECT * FROM database WHERE id >= '".$currentid."' ORDER BY id ASC 그러면 3 대신 2 개의 쿼리 만 사용됩니다.

관련 문제