如何使用 IN () 参数执行 PostgreSQL PREPARE 查询
How to execute a PostgreSQL PREPARE query with IN () parameters
我正在尝试准备来自 PHP 的查询,例如:
pg_prepare($con, "prep", "select * from test where tid in ()");
然后执行:
$strpar = "3,4,6,8,10";
pg_execute($con, "prep", array($strpars));
问题是我无法传递一系列构建的值,因为 prepare 需要固定数量的参数。有没有办法让参数动态化?
使用数组表示一系列值:
pg_prepare($con, "prep", "select * from test where tid=ANY()");
$strpar = "{3,4,6,8,10}";
pg_execute($con, "prep", array($strpars));
您还可以创建一个 PHP 函数来接收 PHP 数组并将其设置为 Postgres 准备语句的有效数组,如:
function php_array_to_pg ($array) {
$values = "";
foreach ($array as $value) {
if ($values=="") {
$values = $value;
} else {
$values = $values.",".$value;
}
}
return "{".$values."}";
}
然后你声明如下:
pg_prepare($con, "prep", "select * from test where tid=ANY()");
$array = array(1,2,3,4);
pg_execute($con, "prep", array(php_array_to_pg ($array)));
我正在尝试准备来自 PHP 的查询,例如:
pg_prepare($con, "prep", "select * from test where tid in ()");
然后执行:
$strpar = "3,4,6,8,10";
pg_execute($con, "prep", array($strpars));
问题是我无法传递一系列构建的值,因为 prepare 需要固定数量的参数。有没有办法让参数动态化?
使用数组表示一系列值:
pg_prepare($con, "prep", "select * from test where tid=ANY()");
$strpar = "{3,4,6,8,10}";
pg_execute($con, "prep", array($strpars));
您还可以创建一个 PHP 函数来接收 PHP 数组并将其设置为 Postgres 准备语句的有效数组,如:
function php_array_to_pg ($array) {
$values = "";
foreach ($array as $value) {
if ($values=="") {
$values = $value;
} else {
$values = $values.",".$value;
}
}
return "{".$values."}";
}
然后你声明如下:
pg_prepare($con, "prep", "select * from test where tid=ANY()");
$array = array(1,2,3,4);
pg_execute($con, "prep", array(php_array_to_pg ($array)));