1

我面临一个问题。我需要使用 PHP 和 MySQL 检查应该出现在两列值中的值。我在下面解释我的表格。

    id zip_from zip_to

     1 751001 751030

db_邮编:

$post_code='751010';
$sql="select * from db_postcode where zip_from >='".$post_code."' and zip_to='".$post_code."'";
$sqlpin=mysqli_query($conn,$sql);
if (mysqli_num_rows($sqlpin) > 0) {
    $data=array("status"=>"Success","msg"=>"Product is available for this postcode");
}else{
    $data=array("status"=>"Failed","msg"=>"Product is not available for this postcode");
}
echo json_encode($data);

在这里,我收到{"status":"Failed","msg":"Product is not available for this postcode"} 错误消息,因为代码751010存在于751001-751030. 在这里,我需要检查用户给定的值是否应该出现在这两列中。

4

2 回答 2

1

你的比较写错了,你想要的是检查和$post_code之间:zip_fromzip_to

$sql="select * from db_postcode where '$post_code' between zip_from and zip_to";

请注意,这仅在 allzip_from和values 的长度相同时才有效zip_to$post_code否则您将遇到字符串比较中的问题,2 > 100000. 如果它们的长度不同,则应将它们转换为整数以进行比较。

于 2019-01-09T12:15:17.627 回答
0

好吧,您的查询应该是这样的:

$sql="select * from db_postcode where ".$post_code." BETWEEN zip_from and zip_to";
于 2019-01-09T12:03:08.757 回答