簡體   English   中英

如何在PHP中制作動態postgres預處理語句

[英]How to make dynamic postgres prepared statements in PHP

我正在嘗試使用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個變量似乎不起作用。 我希望你明白。

我需要在這個周末使用它,所以我希望有人知道!

先感謝您!

在一份聲明中,SQL是有意的靜態 一旦准備好語句,參數的數量就不會改變。

但是,根據語句,您的代碼很容易提交正確數量的參數。 您可以為參數計數器添加變量,並將動態php數組傳遞給pg_execute而不是硬編碼的文字。 並且它們將在if (isset(...))分支內增加/填充。

擁有3個不同的語句(每種情況一個)並執行適用的語句取決於傳遞的參數數量沒有任何錯誤。 例:

編輯:我修改了代碼以匹配所有情況:

  • 只指定了zip
  • 郵編+城市
  • 郵編+國家
  • 郵編+城市+國家

(即使有其他一些案例,你也會理解這個想法)

$stmt = "SELECT * FROM customer WHERE zip = '$1'";

if(isset($_POST["CITY"]) && isset($_POST["COUNTRY"])) { 
   $stmt3 = $stmt . " AND city = '$2'" . " AND country = '$3'";
} elseif(isset($_POST["CITY"])) { 
   $stmt1 = $stmt . " AND city = '$2'";
} elseif(isset($_POST["COUNTRY"])) {
   $stmt2 = $stmt . " AND country = '$2'";
}

if(isset($stmt3)) {
   $result = pg_prepare("myconnection", "my query", $stmt3);
   $result1 = pg_execute("myconnection","my query", array("0000","someCity","someCountry"));
} elseif(isset($stmt2)) {
   $result = pg_prepare("myconnection", "my query", $stmt2);
   $result1 = pg_execute("myconnection","my query", array("0000","someCountry"));
} elseif(isset($stmt1)) {
   $result = pg_prepare("myconnection", "my query", $stmt1);
   $result1 = pg_execute("myconnection","my query", array("0000","someCity"));
} else {
   $result = pg_prepare("myconnection", "my query", $stmt);
   $result1 = pg_execute("myconnection","my query", array("0000"));
}

為了簡潔,我省略了(正如你所做的)所有錯誤檢查。

雖然Daniel和aymeric都是正確的 - 測試兩次,也不使用數字都沒有意義。 見下文:

$some_vars = array();
$some_vars[":zip"] = $_POST["ZIP"];
$stmt = "SELECT * FROM customer WHERE zip = :zip";

if(isset($_POST["CITY"])){ 
    $some_vars[":city"] = $_POST["CITY"]);
    $stmt .= " AND city = :city";
}

if(isset($_POST["COUNTRY"])){ 
    $some_vars[":country"] = $_POST["COUNTRY"]);
    $stmt .= " AND country = :country";
}

$result = pg_prepare("myconnection", "my query", $stmt);
$result1 = pg_execute("myconnection","my query", $some_vars);

不要忘記消毒等。

不要進行字符串連接。 檢查參數是否已設置。 如果沒有將它們設置為空。 使用單個查詢字符串:

$zip = $_POST["zip"];
$city = $_POST["city"];
$country = $_POST["country"];

if (!isset($zip)) $zip = '';
if (!isset($city)) $city = '';
if (!isset($country)) $country = '';

$stmt = "
    select *
    from customer
    where
        (zip = '$1' or '$1' = '')
        and
        (city = '$2' or '$2' = '')
        and
        (country = '$3' or '$3' = '')
";

$result = pg_prepare("myconnection", "my query", $stmt);
$result1 = pg_execute(
        "myconnection",
        "my query",
        array($zip, $city, $country)
        );

僅當相應參數不是空字符串時,才會強制執行每個條件。

相同的邏輯可以使用null值而不是空值,這些列包含應該選擇的空字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM