Search code examples
phppdoprepared-statementexecutebindparam

PHP - PDO Prepared statment, "Warning: PDOStatement::bindParam() expects at least 2 parameters"


i am trying to use a prepared statement to set a placeholder value using a class object property that is passed as an argument to the __construct function. However i seam to be getting an error specifying the need for 2 parameters when i only have one parameter for the place holder value.

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:

Warning: PDOStatement::bindParam() expects at least 2 parameters, 1 given in

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: no parameters were bound'

Solution

Thanks to @Daerik, i changed my bindParam() statements to:

$this->query->bindParam(':full_name', $this->order));

This removed the errors.


Solution

  • PDOStatement::bindParam ( mixed $parameter , mixed &$variable )

    $parameter: Parameter identifier. For a prepared statement using named placeholders, this will be a parameter name of the form :name. For a prepared statement using question mark placeholders, this will be the 1-indexed position of the parameter.

    $variable: Name of the PHP variable to bind to the SQL statement parameter.

    You'll want to use:

    $this->query->bindParam(':full_name', $this->order);
    

    For more information read PDOStatement::bindParam.