如何在带有Postgres的quote中使用参数?我一直收到错误:“SQLSTATE[HY093]:无效参数编号::beginDaysAgo”
当我们看这些线条时:
WHERE a.balance <= (a.autorefill_threshold+:amountAboveThreshold)
AND ((t.created_at <= ( current_timestamp-INTERVAL \':beginDaysAgo days\')) AND ( t.created_at >= (current_timestamp) - INTERVAL \':totalDays days\'))
第一个参数没有为我生成错误。是报价单里面的。
这意味着,第一个参数,金额AboveThreshold工作,但它显然不能在字符串内搜索。
基本上,当我只是在里面使用PHP变量而不是参数时,它工作得非常好,或者当我只是在里面输入一个数字时。例如,当我分别为这两个参数beginDaysAgo和totalDays设置数字20和21时,它工作得非常好。
但当我尝试使用参数时——这是正确和安全的方法——它不起作用。
public function getClientsWithBalanceBelowThreshold(
$amountAboveThreshold=100.00,
$beginDaysAgo = 0,
$amountOfDays = 1
) {
$totalDays = $amountOfDays + $beginDaysAgo;
//this one works
if ((double)$amountAboveThreshold!=$amountAboveThreshold)
throw new \TypeError("Type Mismatch");
$conn = $this->em->getConnection();
$conn = $this->em->getConnection();
$sql = '
SELECT DISTINCT ON (l.public_id) a.balance, a.public_id as account_public_id, a.organization_name, a.autorefill_threshold,
l.name as listing_name, l.paused, l.public_id,
t.balance_before,
t.balance_after, t.created_at, t.type
FROM transaction as t INNER JOIN account a
ON t.account_id = a.account_id
INNER JOIN listing as l ON a.account_id = l.account_id
WHERE a.balance <= (a.autorefill_threshold+:amountAboveThreshold)
AND ((t.created_at <= ( current_timestamp-INTERVAL \':beginDaysAgo days\')) AND ( t.created_at >= (current_timestamp) - INTERVAL \':totalDays days\'))
AND t.balance_before != t.balance_after
AND t.type != \'credit\'
ORDER BY l.public_id, a.balance DESC, t.created_at, l.account_id;
';
$stmt = $conn->prepare($sql);
$stmt->bindParam('amountAboveThreshold', $amountAboveThreshold);
$stmt->bindParam('beginDaysAgo', $beginDaysAgo);
$stmt->bindParam('totalDays', $totalDays);
$stmt->execute();
var_dump($stmt->fetchAll());die;
我收到的全部错误是。。。
“SQLSTATE[HY093]:无效参数编号::beginDaysAgo”#0/var/www/clientrachapi。com/releases/2018_03_10_14_54_58/vendor/doctrine/db al/lib/doctrine/DBAL/Statement。php(141):条令\DBAL\Driver\pdo语句-
我认为当查询中引用了:beginDaysAgo
和:totalDays
时,它们将被解释为文本字符串,而不是参数的占位符。我建议从SQL中删除引号和days
部分,只保留占位符,如下所示:
(t.created_at <= (current_timestamp - INTERVAL :beginDaysAgo))
AND ( t.created_at >= (current_timestamp - INTERVAL :totalDays))
然后在将数值绑定到准备好的语句之前,将天
部分附加到数值:
$beginDaysAgo = "$beginDaysAgo days";
$amountOfDays = "$amountOfDays days";