我使用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 :(得分: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();