PDO 准备语句为不同的查询绑定一次参数
PDO prepared statement bind parameters once for different queries
我正在使用 PDO prepared statements
执行两个查询:
SELECT count(*) FROM vocabulary WHERE `type` = :type AND `lesson` = :lesson;
SELECT * FROM vocabulary WHERE `type` = :type AND `lesson` = :lesson limit 100;
获取计数的第一个查询按预期工作,我得到了行数。
$stmt = $this->connection->prepare($sql);
foreach ($params as $key => $value)
$stmt->bindValue(":" . $key, $value, PDO::PARAM_STR);
$stmt->execute();
$count = $stmt->fetchColumn();
$sql .= " limit $limit;";
$sql = str_replace("count(*)", $columns, $sql);
$stmt = $this->connection->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_CLASS, $class);
但是在执行第二个查询时我得到:
SQLSTATE[HY093]: Invalid parameter number: no parameters were bound
因此,我想知道,如果我有多个参数完全相同的查询,我是否需要使用
再次绑定相同的参数
foreach ($params as $key => $value)
$stmt->bindValue(":" . $key, $value, PDO::PARAM_STR);
或者是否有办法只绑定一次参数。
没有必要像这样修改您的SQL。您的代码基本上可以归结为:
$stmt = $this->connection->prepare('SELECT count(*) FROM vocabulary WHERE `type` = :type AND `lesson` = :lesson');
$stmt->execute($params);
$count = $stmt->fetchColumn();
$stmt = $this->connection->prepare('SELECT * FROM vocabulary WHERE `type` = :type AND `lesson` = :lesson limit 100');
$stmt->execute($params);
$result = $stmt->fetchAll(PDO::FETCH_CLASS, $class);
If I have multiple queries where the parameters are exactly the same, do I need to bind the same parameters again using
当然可以。
参数绑定到每个查询,而不是全局绑定到 PDO 或数据库。
附带说明一下,使用 PDO,您不必显式绑定变量,因此您的 "problem" 有一个解决方案:根本不绑定,而是将您的数据直接发送到 execute( ) 如 Dharman 的出色回答所示
我正在使用 PDO prepared statements
执行两个查询:
SELECT count(*) FROM vocabulary WHERE `type` = :type AND `lesson` = :lesson;
SELECT * FROM vocabulary WHERE `type` = :type AND `lesson` = :lesson limit 100;
获取计数的第一个查询按预期工作,我得到了行数。
$stmt = $this->connection->prepare($sql);
foreach ($params as $key => $value)
$stmt->bindValue(":" . $key, $value, PDO::PARAM_STR);
$stmt->execute();
$count = $stmt->fetchColumn();
$sql .= " limit $limit;";
$sql = str_replace("count(*)", $columns, $sql);
$stmt = $this->connection->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_CLASS, $class);
但是在执行第二个查询时我得到:
SQLSTATE[HY093]: Invalid parameter number: no parameters were bound
因此,我想知道,如果我有多个参数完全相同的查询,我是否需要使用
再次绑定相同的参数foreach ($params as $key => $value)
$stmt->bindValue(":" . $key, $value, PDO::PARAM_STR);
或者是否有办法只绑定一次参数。
没有必要像这样修改您的SQL。您的代码基本上可以归结为:
$stmt = $this->connection->prepare('SELECT count(*) FROM vocabulary WHERE `type` = :type AND `lesson` = :lesson');
$stmt->execute($params);
$count = $stmt->fetchColumn();
$stmt = $this->connection->prepare('SELECT * FROM vocabulary WHERE `type` = :type AND `lesson` = :lesson limit 100');
$stmt->execute($params);
$result = $stmt->fetchAll(PDO::FETCH_CLASS, $class);
If I have multiple queries where the parameters are exactly the same, do I need to bind the same parameters again using
当然可以。
参数绑定到每个查询,而不是全局绑定到 PDO 或数据库。
附带说明一下,使用 PDO,您不必显式绑定变量,因此您的 "problem" 有一个解决方案:根本不绑定,而是将您的数据直接发送到 execute( ) 如 Dharman 的出色回答所示