pdo绑定asc / desc动态 [英] pdo binding asc/desc order dynamically

查看:214
本文介绍了pdo绑定asc / desc动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说,我有2个pdo语句,只有顺序不同(asc和desc)。

Let's say I have 2 pdo statements that differ only in order (asc vs. desc)

$stmt1 = $po->prepare("SELECT * FROM tabname WHERE categ=:categ ORDER BY field1 DESC");
$stmt2 = $po->prepare("SELECT * FROM tabname WHERE categ=:categ ORDER BY field1 ASC");

有一种方法可以动态绑定ASC / DESC,所以我只能有1 stmt

Is there a way I can bind ASC/DESC dynamically so I can have only 1 stmt

$order = "ASC"; //or "DESC"

$stmt = $po->prepare("SELECT * FROM tabname WHERE categ=:categ ORDER BY field1 order=:order");
$stmt->bindParam(':order', $order, PDO::PARAM_STR);


推荐答案

参数将自动引用,ASC / DESC不应引用。这与表和列名称不能是参数的原因相同。

no. parameters are automatically quoted, and ASC/DESC shouldn't be quoted. this is the same reason that table and column names can't be parameters.

这篇关于pdo绑定asc / desc动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆