简体   繁体   English

试图找出PHP PDO准备好的语句和占位符。 MySQL语句“ IN”子句中的占位符

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

I'm new to PDO and prepared statements. 我是PDO和准备好的语句的新手。 Why does this work: 为什么这样做:

$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'];
}

...but this doesn't: ...但这不是:

$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'];
}

I read that you cant use parameters for tables or columns, is this the case with IN clause, too? 我读到您不能为表或列使用参数,IN子句也是如此吗?

Parameter substitution using ? 使用?进行参数替换 is not the same as variable expansion -- which is what it looks like you're expecting in the case that doesn't work. 与变量扩展不同-在不起作用的情况下,这就是您期望的样子。 The actual SQL that gets to the db might look like ... IN ("'blue butterfly wing', 'blue dartwing', 'blue mountain flower'"), so of course it wouldn't work. 进入数据库的实际SQL可能看起来像是... IN(“'blue butterfly wing','blue dartwing','blue mountain flower'“),因此当然不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM