2013-07-16 44 views
1

我一直在絞盡腦汁想要獲得一些看似簡單的工作。我有一個表「重量」。重量有3列「shipping_to」,「shipping_from」和「shipping_cost」。PHP在MySQL中使用大於號和小於號在MySQL中選擇值

Shipping_to和Shipping_from是權重值和運輸成本保持運輸成本,如果該值大於或等於X且小於或等於X

我寫了這個一百萬個不同的方式和我的生活是行不通的。

更新: 腳本工作「種」,但它永遠不會返回1的成功響應,它永遠不會吝嗇X的價值,即使我已經手動將這些值放入我的MySQL分貝。

php腳本:

if($The_Function=="SHIPPING_COST"){ 
    $response = array(); 

    require_once __DIR__ . '/db_connect.php'; 

    $con = new DB_CONNECT(); 

    $ship_weight = 1; 


    $result = mysql_query("SELECT * FROM weight WHERE from_weight >= '$ship_weight' AND to_weight <= '$ship_weight'"); 

    if(!empty($result)){ 
     if (mysql_num_rows($result) > 0) { 
      $response["userID"] = array(); 
      while ($row = mysql_fetch_array($result)) { 
       $custID = array(); 
       $custID["shipping_cost"] = $row["shipping_cost"]; 
       array_push($response["userID"], $custID); 
      } 
      $response["success"] = 1; 

      echo json_encode($response); 
     }else { 
      $response["success"] = 0; 
      $response["message"] = "No shipping found"; 

      echo json_encode($response); 
     } 
    }else { 
     $response["success"] = 0; 
     $response["message"] = "No shipping found"; 

     echo json_encode($response); 
    } 
} 
+0

能否請您詳細說明什麼是不工作的,即:您獲得了什麼價值 - 這種方式有助於我們應該看哪些部分。 – azngunit81

+0

在上面添加了更多信息。 –

+0

from_weight> ='$ ship_weight'AND to_weight <='$ ship_weight' - 哪個重量越來越小? – azngunit81

回答

2

錯誤發生在查詢本身。我所要做的就是給最後一列進行比較,所以我再次添加了$ ship_weight。這是代碼。

$result = mysql_query("SELECT * FROM weight WHERE '$ship_weight' >= from_weight AND '$ship_weight' <= to_weight"); 
0

我認爲這個問題是$result將永遠empty()

如果查詢工作,如果將一個resource handle,如果失敗將是false

那麼試試這個:

$result = mysql_query("SELECT * FROM weight WHERE from_weight >= '$ship_weight' AND to_weight <= '$ship_weight'"); 

if($result !== FALSE){ 
    if (mysql_num_rows($result) > 0) { 
     $response["userID"] = array(); 
     while ($row = mysql_fetch_array($result)) { 
      $custID = array(); 
      $custID["shipping_cost"] = $row["shipping_cost"]; 
      array_push($response["userID"], $custID); 
     } 
     $response["success"] = 1; 

     echo json_encode($response); 
    }else { 
     $response["success"] = 0; 
     $response["message"] = "No shipping found"; 

     echo json_encode($response); 
    } 
}else { 
    $response["success"] = 0; 
    $response["message"] = "No shipping found"; 

    echo json_encode($response); 
} 
+1

'$ a = false; echo empty($ a);'打印1 – Orangepill

+0

我發佈的腳本確實運行,如果沒有任何內容符合查詢請求,$結果將爲空。 –

0

這是您的代碼的拷貝和粘貼?如果是這樣,看看這個行:

$result = mysql_query("SELECT * FROM weight WHERE from_weight >= '$ship_weight' AND to_weight <= '$ship_weight'"); 

PHP正在考慮您的$ship_weight變量作爲字符串的一部分。將其更改爲:

$result = mysql_query("SELECT * FROM weight WHERE from_weight >= '".$ship_weight."' AND to_weight <= '".$ship_weight."'"); 

此外,mysql_ *已被棄用。看看mysqli_* extension