PHP 使用大于和小于在 MySQL 中选择值

标签 php mysql

我一直在绞尽脑汁试图得到一些看起来简单的东西。我有一个“重量”表。重量有 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/

相关文章:

php - 如何使用 PHP 对 CSV 文件进行排序而不对标题行进行排序?

c# - 在不使用 'for' 循环的情况下在 gridView 中插入多行

PHP/MYSQL 按列分组,有 2 个连接表

asp.net - MySQL 与 SQL Server 2005/2008 性能对比

mysql 查询导致带有数字符号的空白页?

php - Facebook Messenger 机器人中的发件人 ID 是否唯一?

php - 如何使用适用于 PHP 的 Google 云存储?

php - fatal error : Cannot use object of type MongoCursor as array

PHP 在解析 XML 时缺少属性

php - 复杂的 mysql 查询将对 2 个表的结果进行排序