PDO Execute显示为字符串形式的Bind Integer参数


PDO Execute appears to Bind Integer parameter as a string

PDO似乎会在准备好的sql语句中自动向绑定参数添加引号。

当我试图限制结果的数量时,我不想要这个功能,因为这应该被视为mysql的整数。

function fixStatistics($metal, $currency, $months){
$db = connectPDO();
$sql = '
        SELECT [COLUMN NAMES]
        FROM [TABLE NAMES]
        WHERE [WHERE STUFF]
        GROUP BY `Month`
        ORDER BY MONTH(DateStamp) LIMIT :numMonths
';
$stmt = $db->prepare($sql);
$stmt->execute(
        array(
        ':metal' => $metal,
        ':currency' => $currency,
        ':numMonths' => $months // Problem Code
    )
);
$statistics = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $statistics;
}

我可以通过手动注入/连接sql语句来解决这个问题——这不是我想做的

ORDER BY MONTH(DateStamp) LIMIT ' . $numMonths

但是,有没有一种方法可以将:numMonths直接转换为$stmt->execute();点的int?

来自关于执行数组param:的文档

具有与绑定参数一样多的元素的值数组在正在执行的SQL语句中。所有值均视为PDO::PARAM_STR。

在执行之前使用这样的东西:

$stmt->bindValue(':numMonths', intval($months), PDO::PARAM_INT);