【问题标题】:PHP Symfony2 Doctrine PDO_PGSQL binding variablesPHP Symfony2 Doctrine PDO_PGSQL 绑定变量
【发布时间】:2017-02-25 02:31:50
【问题描述】:

我的 PHP 代码中有这个查询:

select * from book where title ~* '\mkeyword'; /* \m matches only at the beginning of a word */

关键字是用户输入。如何重写查询以使用绑定变量? 当我这样做时:

select * from book where title ~* :keyword;

然后:

$stmt->bindValue('keyword', "\m".$keyword);

在准备语句之后, 我在查询中得到 \\m 并且查询不会按我想要的方式工作。

【问题讨论】:

    标签: php postgresql symfony pdo doctrine-orm


    【解决方案1】:

    对于名为 :keyword 的参数,您还必须使用相同的名称 - 包括冒号,请参阅 PDOStatement::bindValue 的示例部分

    $sth = $dbh->prepare('SELECT name, colour, calories
        FROM fruit
        WHERE calories < :calories AND colour = :colour');
    $sth->bindValue(':calories', $calories, PDO::PARAM_INT);
    $sth->bindValue(':colour', $colour, PDO::PARAM_STR);
    

    所以在你的情况下,它应该是

    $stmt->bindValue(':keyword', "\m" . $keyword);
    

    作为替代方案,你也可以给PDOStatement::execute一个数组

    $stmt->execute(array(':keyword' => "\m" . $keyword));
    

    【讨论】:

      猜你喜欢
      • 2014-05-26
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      相关资源
      最近更新 更多