我一直在绞尽脑汁试图得到一些看起来简单的东西。我有一个“重量”表。重量有 3 列“shipping_to”、“shipping_from”和“shipping_cost”。
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);
}
}
最佳答案
错误出现在查询本身中。我所要做的就是给最后一列一些东西来比较,所以我只是再次添加了 $ship_weight 。这是代码。
$result = mysql_query("SELECT * FROM weight WHERE '$ship_weight' >= from_weight AND '$ship_weight' <= to_weight");
关于PHP 使用大于和小于在 MySQL 中选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17687777/