【问题标题】:Use php array as parameter for Postgres IN clause使用 php 数组作为 Postgres IN 子句的参数
【发布时间】:2018-05-22 20:20:50
【问题描述】:

我正在尝试将 PHP 数组绑定为 SQL stmt 的参数。我正在使用 auraSQL 扩展 PDO,所以它看起来像这样:

$php_array = ['first', 'second', 'third']
$db->fetchColumn("SELECT * FROM my_table WHERE column IN (:php_array), ['php_array' => $php_array])"

有没有办法做到这一点?我不知道怎么做。我试图将$php_array 附加为用逗号分隔并用引号括起来的字符串,但这不起作用。

编辑:解决方案是像这样使用 AuraSql 函数quote

 $db->fetchColumn("SELECT * FROM my_table WHERE column IN (".$db->quote($php_array).")"; 

【问题讨论】:

标签: php postgresql pdo auraphp


【解决方案1】:

查看quote() 的 Aura.Sql 文档,此处:https://github.com/auraphp/Aura.Sql/blob/3.x/docs/getting-started.md#array-quoting

SQL IN 子句需要逗号分隔的值列表。 Aura.Sql quote() 方法似乎会为您生成。

您应该考虑使用perform 方法。

https://github.com/auraphp/Aura.Sql/blob/3.x/docs/getting-started.md#the-perform-method

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 2021-02-14
    • 2017-04-08
    相关资源
    最近更新 更多