每次准备PDO后都需要绑定params吗?

时间:2016-09-29 04:34:17

标签: php mysql pdo

我对PDO有以下疑问:

如果我将执行多个查询,每次调用prepare时都需要使用bindParam方法?

示例:

$connection->prepare( "SELECT * FROM table WHERE y = :y" );
$connection->bindParam( ":y", $y );

$connection->prepare( "SELECT * FROM table2 WHERE y = :w" );
$connection->bindParam( ":w", $w );

或者我可以使用以下内容做得更好:

$connection->bindParam( ":y", $y );
$connection->bindParam( ":w", $w );

$connection->prepare( "SELECT * FROM table WHERE y = :y" );
$connection->prepare( "SELECT * FROM table2 WHERE y = :w" );

OR:

$connection->prepare( "SELECT * FROM table WHERE y = :y" );
$connection->prepare( "SELECT * FROM table2 WHERE y = :w" );

$connection->bindParam( ":y", $y );
$connection->bindParam( ":w", $w );

¿哪个订单,什么是可能的?

1 个答案:

答案 0 :(得分:3)

当你bind params将它们绑定到准备好的声明而不是连接时,所以这实际上不会起作用

$connection->bindParam( ":w", $w );

相反,您需要这样做:

$stmt = $connection->prepare( "SELECT * FROM table2 WHERE y = :w" );
$stmt->bindParam( ":w", $w );

这自动意味着您需要为每个语句分别绑定参数。