【发布时间】:2017-05-01 10:22:42
【问题描述】:
我正在尝试使用准备好的语句来设置占位符值,该值使用作为参数传递给 __construct 函数的类对象属性。但是,当我只有一个参数作为占位符值时,我会收到一个错误,指定需要 2 个参数。
代码:
<?php include ('connection.inc.php');
class Team {
// Remember to switch back to private!
private $database;
public $statement = 'SELECT * FROM members ORDER BY :full_name';
public $order;
public $query;
private $result;
public function __construct($database, $order) {
$this->database = $database;
$this->order = $order;
$this->query = $this->database->prepare($this->statement);
$this->query->bindParam(array('full_name', $this->order));
$this->query->execute();
}
public function getMember() {
$this->result = $this->query->fetch(PDO::FETCH_ASSOC);
return $this->result;
}
public function id() {
echo $this->result['id'];
}
public function fullName() {
echo $this->result['full_name'];
}
public function alias() {
echo $this->result['alias'];
}
public function abilities() {
echo $this->result['abilities'];
}
};
$test = new Team($database, 'full_name');
?>
错误:
警告:PDOStatement::bindParam() 至少需要 2 个参数,其中 1 个给出
致命错误:未捕获的异常“PDOException”,带有消息“SQLSTATE[HY093]:无效参数号:未绑定参数”
解决方案
感谢@Daerik,我将我的bindParam() 语句更改为:
$this->query->bindParam(':full_name', $this->order));
这消除了错误。
【问题讨论】:
标签: php pdo prepared-statement execute bindparam