php – PDO – execution of a request with a redundant array of parameters

Question:

This code works

    $info = array();
    $info[':nik'] = trim($_POST['nik']);
    $info[':phone'] = trim($_POST['phone']);
    $info[':FIO'] = trim($_POST['FIO']);
    $info[':email'] = trim($_POST['email']);

    $sql = "select count(*) from `punbb_users` where `email` = :email limit 1";
    $count = $DB->getOne($sql, array(':email' => $info[':email']));

But if you replace the last line:

 $count = $DB->getOne($sql, $info);

Then nothing works.

The $DB->getOne() function itself prepares and executes the request via pdo

public function getOne($sql, $params = NULL) {
    if (is_null($params)) {
        return $this->pdo->query($sql)->fetchColumn();
    }
    else {
        $stmt = $this->prepare($sql);
        $stmt->execute($params);
        return $stmt->fetchColumn();
    }
}

public function prepare($sql) {
    return $this->pdo->prepare($sql);
}

This comes from the fact that you cannot pass an array with other parameters when you only need one, or am I doing something wrong?

A link to the documentation is also desirable, if this cannot be done.

Answer:

Yes, the number of parameters in the request and in the array that is passed to the prepare() method must match, otherwise the following error should be returned to you

SQLSTATE [HY093]: Invalid parameter number: number of bound variables does not match number of tokens

Scroll to Top