【发布时间】:2012-08-17 16:06:01
【问题描述】:
我正在尝试使用 postgres 在 PHP 中编写一些准备好的语句。
这有点难解释,所以我就给你演示一下:
$stmt = "SELECT * FROM customer WHERE zip = '$1'";
if(isset($_POST["CITY"])){
$stmt .= "AND city = '$2'";
}
if(isset($_POST["COUNTRY"])){
$stmt .= "AND country = '$3'";
}
$result = pg_prepare("myconnection", "my query", $stmt);
$result1 = pg_execute("myconnection","my query", array("0000","someCity","someCountry"));
抱歉,如果某些代码有误,但这是一个徒手示例。我需要的是能够根据某些变量 isset/not-null 使准备好的语句动态化。 当语句只需要 1 或者我只需要添加 $1 和 $3 而不是 $2 时,在数组中发布 3 个变量时,它似乎不起作用。希望你能理解。
这个周末要用,希望有人知道!
提前谢谢你!
【问题讨论】:
标签: php postgresql prepared-statement