定义列名并限制查询中的数据

时间:2015-07-01 17:25:45

标签: php mysql pdo

我使用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个结果吗?

1 个答案:

答案 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();