簡體   English   中英

試圖找出PHP PDO准備好的語句和占位符。 MySQL語句“ IN”子句中的占位符

[英]Trying to figure out PHP PDO prepared statements and placeholders. Placeholder in “IN” clause of MySQL statement

我是PDO和准備好的語句的新手。 為什么這樣做:

$sth = $dbh->prepare("SELECT * 
                      FROM skyrim_ingredients 
                      WHERE ing_name 
                      IN ('blue butterfly wing', 'blue dartwing', 'blue mountain flower');");
while($row = $sth->fetch()) {
    echo $row['ing_id'];
}

...但這不是:

$ing_string = 'blue butterfly wing', 'blue dartwing', 'blue mountain flower';
$sth = $dbh->prepare("SELECT * 
                      FROM skyrim_ingredients 
                      WHERE ing_name 
                      IN (?);");
$sth->bindParam(1, $ing_string);
$sth->execute();
while($row = $sth->fetch()) {
    echo $row['ing_id'];
}

我讀到您不能為表或列使用參數,IN子句也是如此嗎?

使用?進行參數替換 與變量擴展不同-在不起作用的情況下,這就是您期望的樣子。 進入數據庫的實際SQL可能看起來像是... IN(“'blue butterfly wing','blue dartwing','blue mountain flower'“),因此當然不起作用。

暫無
暫無

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

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