簡體   English   中英

無法使用 MySQL 查詢檢查兩列內的值

[英]Could not check value within two column using MySQL query

我面臨一個問題。 我需要使用 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 在這里,我需要檢查用戶給定的值是否應該存在於這兩列中。

你的比較寫錯了,你想要的是檢查$post_code是否在zip_fromzip_to之間:

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

請注意,這僅在所有zip_fromzip_to$post_code值的長度相同$post_code ,否則您將遇到字符串比較中的問題, 2 > 100000 如果它們的長度不同,則應將它們轉換為整數以進行比較。

好吧,您的查詢應該是這樣的:

$sql="select * from db_postcode where ".$post_code." BETWEEN zip_from and zip_to";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM