[英]Could not check value within two column using MySQL query
I am facing one issue.我面临一个问题。 I need to check value which should present within two column values using PHP and MySQL.
我需要使用 PHP 和 MySQL 检查应该出现在两列值中的值。 I am explaining my table below.
我在下面解释我的表格。
id zip_from zip_to 1 751001 751030
db_postcode: 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);
Here I am getting the message {"status":"Failed","msg":"Product is not available for this postcode"}
which is wrong because code 751010
is present within 751001-751030
.在这里,我收到消息
{"status":"Failed","msg":"Product is not available for this postcode"}
这是错误的,因为代码751010
存在于751001-751030
。 Here I need to check the user given value should be present within that two column.在这里,我需要检查用户给定的值是否应该存在于这两列中。
Your compare is written the wrong way around, what you want is to check that $post_code
is between zip_from
and zip_to
:你的比较写错了,你想要的是检查
$post_code
是否在zip_from
和zip_to
之间:
$sql="select * from db_postcode where '$post_code' between zip_from and zip_to";
Note that this will only work if all zip_from
, zip_to
and $post_code
values are the same length, otherwise you will run into issues that in a string compare, 2 > 100000
.请注意,这仅在所有
zip_from
、 zip_to
和$post_code
值的长度相同$post_code
,否则您将遇到字符串比较中的问题, 2 > 100000
。 If they are not the same length, you should cast them to integer to compare them.如果它们的长度不同,则应将它们转换为整数以进行比较。
好吧,您的查询应该是这样的:
$sql="select * from db_postcode where ".$post_code." BETWEEN zip_from and zip_to";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.