내 페이지가 이미지를 표시하고 현재 페이지와 관련된 이전 이미지와 다음 이미지를 표시하려고합니다. 지금은 동일한 쿼리를 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
마다 할 필요가 없습니다 시간
보다 작거나 큰 연산자 대신 평등을 사용할 수 있습니다. 나는 @Shaune이 이것에 대답 할 수 있었다고 생각한다. 그렇다면 ** 주문 ** 및 ** 제한 **을 삭제할 수 있습니다. 또한 yourtable.id에 indexd가 있습니까? –