-1

试图在购物车中提取报价值的直方图。如果我将它直接粘贴到 mySQL 中,则此查询有效,但是我遇到了 PHP 方面的问题。

错误信息:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in .../.../... Line 26.

我的代码:

echo '<h2>Histogram of Quotes</h2>';

$sql = 'SELECT 
            ROUND(ROUND(Fixes.FixAM/31.1035 * Products.Fineness * Products.Buy * Quotes.Weight, 2), -3)    AS bucket,
            COUNT(*) AS Count,
            RPAD('', LN(COUNT(ROUND(Fixes.FixAM/31.1035 * Products.Fineness * Products.Buy * Quotes.Weight, 2))), "*") AS bar
        FROM   
            Quotes,
            Products,
            Fixes,
            Currencies,
            Metals,
            ProductTypes
        WHERE
            Quotes.ProductId = Products.Id AND
            Products.MetalId = Metals.Id AND
            Products.ProductTypeId = ProductTypes.Id AND
            Fixes.CurrencyId = Currencies.Id AND
            Fixes.MetalId = Metals.Id AND
            Currencies.Code = "GBP"                     
        GROUP BY bucket';

$stmt = $db->prepare($sql); 
$stmt->execute();
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);  

第26行是说GROUP BY bucket';

证明 SQL 代码有效

bucket  COUNT   bar
0       114     *****
1000    37      ****
2000    8       **
3000    2       *
4000    3       *
5000    4       *
8000    1   
9000    1   
10000   1   
21000   1   
4

1 回答 1

3

您正在混合引号而不转义它们:

$sql = 'SELECT 
            ROUND(ROUND(Fixes.FixAM/31.1035 * Products.Fineness * Products.Buy * Quotes.Weight, 2), -3)    AS bucket,
            COUNT(*) AS Count,
            RPAD('', LN(COUNT(ROUND(Fixes.FixAM/31.1035 * Products.Fineness * Products.Buy * Quotes.Weight, 2))), "*") AS bar
// Here ---------^^
        FROM   
            Quotes,
            Products,
            Fixes,
            Currencies,
            Metals,
            ProductTypes
        WHERE
            Quotes.ProductId = Products.Id AND
            Products.MetalId = Metals.Id AND
            Products.ProductTypeId = ProductTypes.Id AND
            Fixes.CurrencyId = Currencies.Id AND
            Fixes.MetalId = Metals.Id AND
            Currencies.Code = "GBP"                     
        GROUP BY bucket';

将它们切换为双引号或在查询中用反斜杠转义它们。

于 2013-01-09T16:34:50.277 回答