如何进行有限的记录搜索?

时间:2015-05-05 18:18:10

标签: php mysql range

请帮帮我。在此代码中,用户将键入一个数字。然后在提交输入后,它应该搜索的唯一记录仅来自ID号1到143(不包括数字144和更高)。我该怎么做?它是否像一个范围?怎么样?谢谢!

<?php


$prevalue = $_POST['id_checknumber'];






$query = mysqli_query($link, "SELECT * FROM demo WHERE id_ln = '$prevalue'");

$numrows = mysqli_num_rows($query);



if ($prevalue <= 10){ 
    echo "<script>
             alert('Loric Number $prevalue has already been taken by a Garde.'); 
             window.history.go(-1);
    </script>";}
    else {
if ($numrows!=0){

while($row = mysqli_fetch_assoc($query)) {
   $id_ln = $row['id_ln'];
   $id_email = $row['id_email'];
   $id_firstname = $row['id_firstname']; 
   $id_lastname = $row['id_lastname']; 
   echo "<script>
             alert('Hi, $id_firstname $id_lastname! Your registered Loric Number is $id_ln. Thanks!'); 
             window.history.go(-1);
    </script>";
}}


if ($numrows===0){echo"<script>
             alert('Hi! This number is not yet registered. Register now! Go back to the main menu!'); 
             window.history.go(-1);
    </script>";
    }
}

mysqli_close($link);

?>

1 个答案:

答案 0 :(得分:0)

$query = mysqli_query($link, "SELECT * FROM demo WHERE id_ln = '$prevalue' AND id_ln <= 143");

我认为这应该回答你的问题,但正如人们在评论中已经说过的那样,在尝试查询之前,最好不要使用高于143的值。

除非您只想要返回前143条记录,否则它将如下所示:

$query = mysqli_query($link, "SELECT * FROM demo WHERE id_ln = '$prevalue' LIMIT 143");