php - MySql Invalid parameter number: number of bound variables does not match number of tokens

Solution:

A note I found on php.net from 10 years ago:

Note that you must

  • EITHER pass all values to bind in an array to PDOStatement::execute()
  • OR bind every value before with PDOStatement::bindValue(), then call PDOStatement::execute() with no parameter (not even "array()"!). Passing an array (empty or not) to execute() will "erase" and replace any previous bindings (and can lead to, e.g. with MySQL, "SQLSTATE[HY000]: General error: 2031" (CR_PARAMS_NOT_BOUND) if you passed an empty array).

https://www.php.net/manual/en/pdostatement.execute.php

As frustrating as this may be (because why not!?), you cannot mix 'bind' and 'execute' when using PDO. Two of your parameters are being deleted. Either pass the tokens in the execute, or bind them all first. (I'd probably bind them first and in order.)

$query->bindValue(':active', 'yes', PDO::PARAM_STR);
$query->bindValue(':lib', $colour, PDO::PARAM_STR);
$query->bindValue(':start', $start, PDO::PARAM_INT);
$query->bindValue(':limit', 'first', PDO::PARAM_INT);
$query->execute();

If binding doesn't work, try putting them into the execute (). If you do this though, be mindful of PHP PDO apparent issue with limit.

https://phpdelusions.net/pdo#limit

Answer

Solution:

The order of the mysql command are wrong, ORDER BY is behind the WHERE Clause

$query = $this->connect()->prepare(
        "SELECT *
        FROM t_event join t_type_event using(tte_id)            
        WHERE eve_active = :active AND tte_lib = :lib
        ORDER BY eve_date DESC
        LIMIT  :start,:limit;"
    );
    $sth->bindValue(':active', 'yes', PDO::PARAM_STR);
    $sth->bindValue(':lib', 'first', PDO::PARAM_STR);
    $sth->bindValue(':limit', $limit, PDO::PARAM_INT);
    $sth->bindValue(':start', $start, PDO::PARAM_INT);
    $query->execute();

And bind all 4 variables before executing.

Source