【问题标题】:php mysql case syntax mistakephp mysql case 语法错误
【发布时间】:2015-06-16 06:00:39
【问题描述】:

这是opencart的一部分:

model/catalog/product.php

if (!empty($data['filter_manufacturer_id'])) {
  $sql .= " AND p.manufacturer_id = '" . (int)$data['filter_manufacturer_id'] . "'";
}

$sql .= " GROUP BY p.product_id";

$sort_data = array(
    'pd.name',
    'p.model',
    'p.quantity',
    'p.price',
    'rating',
    'p.sort_order',
    'p.date_added'
);

if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
    if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') {
        $sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
    } elseif ($data['sort'] == 'p.price') {
        $sql .= " ORDER BY (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END)";
    } else {
        $sql .= " ORDER BY " . $data['sort'];
    }
} else {
    $sql .= " ORDER BY p.sort_order";
}

我正在尝试创建“价格范围”过滤器。这是我的代码:

    //Filter products based on slider price range

    if ((isset($this->request->get['lower']))&&(isset($this->request->get['higher'])))
    {
    $sql .=  " AND p.price >='". $this->request->get['lower'] ." ' AND p.price <='". $this->request->get['higher'] ."'" ;
    }

    //Filter products based on price slider

if (!empty($data['filter_manufacturer_id'])) {
    $sql .= " AND p.manufacturer_id = '" . (int)$data['filter_manufacturer_id'] . "'";
}

$sql .= " GROUP BY p.product_id";

$sort_data = array(
    'pd.name',
    'p.model',
    'p.quantity',
    'p.price',
    'rating',
    'p.sort_order',
    'p.date_added'
);

if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
    if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') {
        $sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
    } elseif ($data['sort'] == 'p.price') {
        $sql .= " ORDER BY (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END)";
    } else {
        $sql .= " ORDER BY " . $data['sort'];
    }
} else {
    $sql .= " ORDER BY p.sort_order";
}

有效!但是当我们商店的产品有特价时,它就不起作用了。所以我试图从SORT BY PRICE 复制一些代码。我复制了这个 SQL:

(CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END)

到我的自定义代码:

    //Filter products based on slider price range

    if ((isset($this->request->get['lower']))&&(isset($this->request->get['higher'])))
    {
    $sql .=  " AND (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END) >='". $this->request->get['lower'] ." ' AND (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END) <='". $this->request->get['higher'] ."'" ;
    }

    //Filter products based on price slider

但还是不行。我收到此错误:

注意:错误:“where 子句”中的未知列“特殊” 错误号:1054

【问题讨论】:

  • 错误信息很清楚:您没有该名称的列
  • 那么你有那个名为special的列吗?

标签: php mysql sql syntax case


【解决方案1】:

以下是我将采取的故障排除步骤:

  1. 确保 special 包含在您的 SELECT 子句中
  2. 确保您不需要使用表前缀,例如p
  3. 在进行 SQL 比较时可能不应引用数字

CASE 表达式的格式似乎正确,所以我认为表达式不是问题。

重要提示:我强烈建议您永远不要在生产环境中使用上述代码。通过采用未经过滤/验证/转义的 GET 参数并将它们添加到查询字符串中,您对 SQL 注入敞开了大门。

【讨论】:

    【解决方案2】:

    检查表的列列表中是否存在“特殊”列。 或在列名前使用别名 (p.special)。

    检查这两个选项。

    【讨论】:

      猜你喜欢
      • 2013-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多