fstdv
fstdv

Reputation: 63

Concat query in php with mysql (PDO object)

I have the following query from filters param:

select * from table WHERE x= :x '{$this->getQuery()}'

function getQuery()
{
  $query = "";
  if ($this->y != "")
    $query ="and y =:y";

  return $query;
}

But sometimes when I got empty params from "Y" from the request I removed the line " and y =:y" and the query will be:

select * from table WHERE x= :x

the problem is that i m using . PDO->bind(:y) and when the query is empty the bind is not working and throw the following error:

 Invalid parameter number: number of bound variables does not match number of tokens

Upvotes: 0

Views: 330

Answers (1)

vjy tiwari
vjy tiwari

Reputation: 861

You have to check the condition when you are binding params .

if($this->getQuery()!=''){
     PDO->bind(':y',$val_y);
}

Upvotes: 1

Related Questions