mysql 准备好的语句是否有编号的占位符
Are there numbered placeholders for mysql prepared statement
我正在尝试使用 mysql 准备好的语句。
我想知道是否有像我可以在 sprintf 或 vsprintf 中使用的编号占位符的可能性。
例如:
<?php
$format = 'The %2$s contains %1$d monkeys';
echo sprintf($format, $num, $location);
?>
这取决于您在 PHP 中使用的 SQL 库。
如果您使用 PDO
库,您可以使用命名参数,这与使用数字是一样的。 http://php.net/manual/en/pdostatement.bindparam.php 给出了一个例子:
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour');
$sth->bindParam(':calories', $calories, PDO::PARAM_INT);
$sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12);
$sth->execute();
在上面,由于参数已命名,因此以何种顺序提供 "calories" 和 "colour" 变量或它们在语句中出现的位置都无关紧要。
但是 mysqli
不支持这一点,您必须使用简单的 ?
占位符,然后按照要使用的确切顺序提供参数 - 请参阅 http://php.net/manual/en/mysqli-stmt.bind-param.php.
我正在尝试使用 mysql 准备好的语句。 我想知道是否有像我可以在 sprintf 或 vsprintf 中使用的编号占位符的可能性。 例如:
<?php
$format = 'The %2$s contains %1$d monkeys';
echo sprintf($format, $num, $location);
?>
这取决于您在 PHP 中使用的 SQL 库。
如果您使用 PDO
库,您可以使用命名参数,这与使用数字是一样的。 http://php.net/manual/en/pdostatement.bindparam.php 给出了一个例子:
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour');
$sth->bindParam(':calories', $calories, PDO::PARAM_INT);
$sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12);
$sth->execute();
在上面,由于参数已命名,因此以何种顺序提供 "calories" 和 "colour" 变量或它们在语句中出现的位置都无关紧要。
但是 mysqli
不支持这一点,您必须使用简单的 ?
占位符,然后按照要使用的确切顺序提供参数 - 请参阅 http://php.net/manual/en/mysqli-stmt.bind-param.php.