语法错误调试 - 意外T_CONSTANT_ENCAPSED_STRING

时间:2013-01-09 16:31:29

标签: php mysql syntax pdo

尝试在购物车中提取报价值的直方图。如果我将它直接粘贴到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   

1 个答案:

答案 0 :(得分: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';

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