shablm
shablm

Reputation: 25

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

I do not know what I am doing wrong here? Can someone please help me? When the following query is executed in Drupal7 custom module, I get the following error:

ERROR:

ResponseText: PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens: 
SELECT t.tid AS tid, t.name AS name
FROM 
{taxonomy_term_data} t
WHERE  (t.vid = :vid) AND (LOWER(t.name) LIKE LOWER('%%:last_string%%')) 
LIMIT 50 OFFSET 0; 
Array
(
  [:vid] => 6
  [:last_string] => server
)

CODE:

$result = db_select('taxonomy_term_data', 't')
            ->fields('t', array('tid', 'name'))
            ->where('t.vid = :vid', array(':vid'=>$vid))
            ->where("LOWER(t.name) LIKE LOWER('%%:last_string%%')", array(':last_string'=>$last_string))
            ->range(0, $num_results)
            ->execute();

The query works if I directly hard code the value for :last_string,

Example: ->where("LOWER(t.name) LIKE LOWER('%%server%%')")

any help is much appreciated..

Upvotes: 2

Views: 353

Answers (1)

Ardit Meti
Ardit Meti

Reputation: 581

Try using only one % because: % is a substitute for zero or more characters. You don't need 2 of them.

The LOWER function takes a string as parameter and '%:last_string%' is taken as string not as a binding to the array(':last_string'=>$last_string), that's why it works when you remove the binding. So try to not put :last_string inside the LOWER function because it won't recognize it as a binding.

Upvotes: 1

Related Questions