如何在php中获取第一个和最后一个记录

时间:2016-03-31 08:05:01

标签: php mysql

我正在尝试在我的页面中创建上一个和下一个记录超链接。必须显示完整的记录。我很好地做到了这一点,但是在达到第一条记录或最后一条记录后,如果前一条或最后一条链接被按下,它就会给我错误。如何解决这个问题。 TIA。我的代码是

$prevquery= "SELECT * FROM SKU_det WHERE bi_SKU < $SKU ORDER BY bi_SKU DESC LIMIT 1"; 
    $prevresult = mysql_query($prevquery) or die(mysql_error());
    while($prevrow = mysql_fetch_array($prevresult))
    {
    $previd  = $prevrow['bi_SKU'];
    }


$nextquery= "SELECT * FROM SKU_det WHERE bi_SKU> $SKU ORDER BY bi_SKU ASC LIMIT 1"; 
$nextresult = mysql_query($nextquery) or die(mysql_error());
while($nextrow = mysql_fetch_array($nextresult))
{
$nextid  = $nextrow['bi_SKU'];
}
echo $SKU;
if ($SKU==0){
    echo "<strong> < Previous</a>";
    echo "<strong> Next ></a>";
}
else if($SKU<>0){
    echo "<a href='CopyAdd.php?SKU=$previd'><strong> < Previous</a>";
    echo "<a href='CopyAdd.php?SKU=$nextid'><strong> Next ></a>";
}`

2 个答案:

答案 0 :(得分:2)

你必须检查你是否有$ previd(或$ nextid) 如果你没有,只需隐藏prev(或下一个)链接

$prevquery= "SELECT * FROM SKU_det WHERE bi_SKU < $SKU ORDER BY bi_SKU DESC LIMIT 1"; 
$prevresult = mysql_query($prevquery) or die(mysql_error());
while($prevrow = mysql_fetch_array($prevresult))
{
  $previd = null;
  if (array_key_exist('bi_SKU', $prevrow) && is_int($prevrow['bi_SKU'])) {
    $previd  = $prevrow['bi_SKU'];
  }
}

$nextquery= "SELECT * FROM SKU_det WHERE bi_SKU> $SKU ORDER BY bi_SKU ASC LIMIT 1"; 
$nextresult = mysql_query($nextquery) or die(mysql_error());
while($nextrow = mysql_fetch_array($nextresult))
{
  $nextid = null;
  if (array_key_exist('bi_SKU', $nextrow) && is_int($nextrow['bi_SKU'])) {
    $nextid  = $nextrow['bi_SKU'];  
  }
}
echo $SKU;
if ($SKU==0){
    echo "<strong> < Previous</a>";
    echo "<strong> Next ></a>";
}
else if($SKU<>0){
    if ($previd !== null) {
        echo "<a href='CopyAdd.php?SKU=$previd'><strong> < Previous</a>";
    }
    if ($nextid !== null) {
        echo "<a href='CopyAdd.php?SKU=$nextid'><strong> Next ></a>";
    }
}`

答案 1 :(得分:1)

为了帮助您以更好的方式编写代码,我做了一些更改,因为如果您继续使用wlalele给出的示例,您很快就会遇到Mysql注入和不推荐使用的mysql函数的问题。

if (isset($SKU)){ //check if SKU is set before creating query.
   $prevquery= "SELECT bi_SKU FROM SKU_det WHERE bi_SKU < '" . mysql_real_escape_string($SKU). "' ORDER BY bi_SKU DESC LIMIT 1";  //escape external input
    $prevresult = mysqli_query($prevquery) or die(mysqli_error());
    if ($prevresult && mysqli_num_rows($prevquery)){ // check if there is a result
        $prevrow = mysqli_fetch_assoc($prevresult))  { // you are requesting 1 record, no need for while loop, also assoc array because you dont need array
        $previd  = $prevrow['bi_SKU'];
    }
    $nextquery= "SELECT bi_SKU FROM SKU_det WHERE bi_SKU > '" . mysql_real_escape_string($SKU). "' ORDER BY bi_SKU DESC LIMIT 1"; 
    $nextresult = mysqli_query($nextquery) or die(mysqli_error());
    if ($nextresult && mysqli_num_rows($nextresult)){ // check if there is a result
        $nextrow = mysqli_fetch_assoc($nextresult))  { // you are requesting 1 record, no need for while loop, also assoc array because you dont need array
        $nextid  = $nextrow['bi_SKU'];
    }
    echo htmlSpecialChars($SKU); //make sure to escape external input
    if (!isset($previd)){
    //only if a previd exists, show button
     echo '<a href="CopyAdd.php?SKU='.$previd.'"><strong> &lt; Previous</strong></a>'; //corrected your html
    }
    if (!isset($nextid)){
    //only if a nextid exists, show button
     echo '<a href="CopyAdd.php?SKU='.$nextid.'"><strong> Next &gt;</strong></a>'; //corrected your html
    }
} else {
    echo 'No SKU set';
    //make a query to fetch first SKU and only show NEXT > for example.
}