2015-07-01 9 views
-3

我使用PDO bindParam()可以在查詢中定義列名。定義查詢中的列名和限制數據

這是我到目前爲止

編輯嘗試:

$item = "Fruits"; 
$num = "5"; 
$tot = (int)$num; 

$sql = "SELECT categories.item, price.:itm 
      FROM categories 
       LEFT JOIN price 
        ON categories.iID = price.iID 
         WHERE price.:itm = :availability 
          ORDER BY categories.item 
           ASC LIMIT :lmt"; 
$stmt = $connect->prepare($sql); 
$stmt->bindParam(':itm', $item); 
$stmt->bindParam(':lmt', $tot); 
$stmt->execute(array(':availability'=>"1")); 
$mulu = $statementa->fetchColumn(); 

我怎麼能算和限制5分的結果?

+0

5的結果是什麼?您還錯過了'ORDER BY'子句 –

+0

學習SQL - 'ORDER BY%FIELD_NAME%ASC' –

+0

@u_mulder:這是我的實際sql。我更新了我的帖子。 – Becky

回答

1

您不能綁定列,只值:

$sql = "SELECT categories.item, price.$item 
      FROM categories 
       LEFT JOIN price 
        ON categories.iID = price.iID 
         WHERE price.$item = :availability 
          ORDER BY categories.item 
           ASC LIMIT :lmt"; 
$stmt = $connect->prepare($sql); 
#$stmt->bindParam(':itm', $item); 
$stmt->bindParam(':lmt', $tot); 
$stmt->execute(array(':availability'=>"1")); 
$mulu = $statementa->fetchColumn(); 
+0

另外'SELECT categories.item,price。:itm' –

+0

謝謝。如果有兩個或兩個以上的人有約束力,你會寫嗎?這可以做爲一個數組嗎? '$ stmt-> bindParam(array(':lmt'=> $ tot,);' – Becky

+0

只需添加儘可能多的'$ stmt-> bindParam(':lmt1',$ tot);','$ stmt - > bindParam(':lmt2',$ tot);'etc ... – meda