我正在尝试使用准备好的语句来使用作为参数传递给类的对象属性来设置占位符值__construct功能。然而,我缝隙得到一个错误,指定需要2个参数,当我只有一个参数的占位符值。PHP-PDO Prepared statment,“警告:PDOStatement :: bindParam()需要至少2个参数”
CODE:
<?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');
?>
ERRORS:
警告:PDOStatement对象:: bindParam()期望至少2个参数,1
致命错误给出:带有消息'SQLSTATE [HY093]的未捕获异常'PDOException':无效的参数编号:无参数ERS被束缚”
解决方案
感谢@Daerik,我改变了我的bindParam()
语句:
$this->query->bindParam(':full_name', $this->order));
此删除这些错误。
谢谢,删除错误和。但是,当使用'gettMember()'方法迭代sql查询时,它将接受忽略我的'ORDER BY':full_name''语句并使用'id'列对条目进行排序。这是我准备好的陈述中的错误吗? –
@ FrederickM.Rogers您将要在'ORDER BY'语句中使用一列。分配一个字符串会将它们全部比较为相同的值,并默认为索引。阅读http://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html了解更多信息。 – Daerik
谢谢你会通读它并做出必要的调整! –