dsfds4551 2012-11-27 14:08
浏览 226
已采纳

在sql中多次使用相同的预处理语句参数

i'm trying to use a same parameter in my sql, but it's only recognizing the first. look my code:

$stmt = $dbh->prepare("SELECT
    (SELECT SUM(oq.value)
    FROM operations_quotas AS oq
        JOIN operations AS o ON oq.operation = o.id
    WHERE o.user = :user AND o.type = 1 AND oq.status = 1
    ) AS total_incomes_open,

    (SELECT SUM(oq.value)
    FROM operations_quotas AS oq
        JOIN operations AS o ON oq.operation = o.id
    WHERE o.user = :user AND o.type = 1 AND oq.status = 2
    ) AS total_incomes_wroteoff");

$stmt->bindParam(":user", $this->getId());
$stmt->execute();

is that possible?

  • 写回答

1条回答 默认 最新

  • doushi9856 2012-11-27 14:14
    关注

    It's not possible to reuse parameters like that. You'll have to make unique parameters:

    $stmt = $dbh->prepare("SELECT
        (SELECT SUM(oq.value)
        FROM operations_quotas AS oq
            JOIN operations AS o ON oq.operation = o.id
        WHERE o.user = :user_a AND o.type = 1 AND oq.status = 1
        ) AS total_incomes_open,
    
        (SELECT COUNT(oq.id)
        FROM operations_quotas AS oq
            JOIN operations AS o ON oq.operation = o.id
        WHERE o.user = :user_b AND o.type = 1 AND oq.status = 2
        ) AS total_incomes_wroteoff");
    
    $stmt->bindParam(":user_a", $this->getId());
    $stmt->bindParam(":user_b", $this->getId());
    $stmt->execute();
    

    From the Manual:

    You must include a unique parameter marker for each value you wish to pass in to the statement when you call PDOStatement::execute(). You cannot use a named parameter marker of the same name twice in a prepared statement. You cannot bind multiple values to a single named parameter in, for example, the IN() clause of an SQL statement.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计