似乎有一个(防?)模式的东西在使用这样的绑定参数:相同的值可以在查询中绑定多次吗?
$dbh->do(
'select foo from bar where baz > ? and baz < ? + 1',
undef,
$var1, $var1
)
即什么是真正想要的是在查询中使用的$var1
值的两倍。有没有办法避免在绑定参数列表中两次指定$var1
?
似乎有一个(防?)模式的东西在使用这样的绑定参数:相同的值可以在查询中绑定多次吗?
$dbh->do(
'select foo from bar where baz > ? and baz < ? + 1',
undef,
$var1, $var1
)
即什么是真正想要的是在查询中使用的$var1
值的两倍。有没有办法避免在绑定参数列表中两次指定$var1
?
首先,使用实际上是
$dbh->do(
'select foo from bar where baz > ? and baz < ? + 1',
undef,
$var1, $var1
);
除非你DBD支持positional or named placeholders,
$dbh->do(
'select foo from bar where baz > $1 and baz < $1 + 1',
undef,
$var1
);
你需要指定两次吧,虽然你可以使用
$dbh->do(
'select foo from bar where baz > ? and baz < ? + 1',
undef,
($var1)x2
);
一种方法可能是使用编号占位符,如果您的驱动程序支持它们,如:
$dbh->do(
'select foo from bar where baz > ?1 and baz < ?1 + 1',
undef,
$var1
)
你想要做的是使用命名参数。某些图书馆,如PDO support this。