开发者

Problem passing PDO::PARAM_INT using array of insert values

I'm having a problem where PDO encapsulates int values using quotes and subsequently makes queries fail.

This is the code (using a wrapper function)

$newest = query("SELECT id, body, upvotes, downvotes 
                FROM suggestions ORDER BY timestamp DESC LIMIT :min, :max",
    array(
        ':min' => $min,
        ':max' => $max
    )
);

And this is the resulting bad query which causes an error (notice the quotes around the LIMIT values)

SELECT id, body, upvotes, downvotes FROM suggestions ORDER BY timestamp DESC LIMIT '0' , '50'

I'm passing an array of 开发者_如何学Govalues:

array(
    ':min' => $min,
    ':max' => $max
)

They're both INTs, and from what I read on the internet PDO should automatically find that out and use PDO::PARAM_INT when binding them. The problem is it doesn't actually do that, and since my only way to pass them is through an array I'd like to ask if there's a way to force them to be PDO::PARAM_INT without having to use bindParam().

Thanks in advance.


If you use a wrapper and you cannot use bindParam then you can do :

$min=(int)$min;
$max=(int)$max;
query("SELECT id, body, upvotes, downvotes 
                FROM suggestions ORDER BY timestamp DESC LIMIT $min, $max");

This is not the best idea. If you want to keep standards you should improve your wrapper to handle such cases.

Like add a 3rd param in your wrapper

function query($q,$paramArray,$bindParamArray) {}

So you can effectively use bindParam too

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜