Like postgres PDO with foreach

Asked

Viewed 79 times

0

I need to use a like in foreach but without success, I did as follows:

$arr = array('\'%a%\'', '\'%b%\'', '\'%c%\'');
foreach ($arr as $a){
   $this->SQL = "update client set active=1 where date between ? and ? and type like ?"
   $this->SQL =$this->PDO->prepare($this->SQL);
   $this->SQL->execute(array($this->Since, $this->Until, $a));
}

It runs but does not update, I tried with var_dump to see what happened, but I was not successful. Someone who’s been through it and managed to work it out?

Thank you =]

  • Thanks for the collaboration, but I was only wrong to pass the example here, in the original code this with the 'and'. abç

  • You don’t need to escape the jokers, see

  • no escape returns like this (my production example): Array ( [0] => 42601 [1] => 7 [2] => ERROR: syntax error at or near "$3" LINE 1: ...t = 0 Where cmdatecreate between $1 and $2 and cmtext in $3 ^ )

  • It says that the error is in the value passed to the like or before.

  • $1 and $2 I know it’s not the problem, in the third step as ta the above array without the escapes, to try with foreach ( $arr as $a => $value)

1 answer

1


If someone goes through the same problem follows how I managed to solve:

$arr = array('a', 'b', 'c');
foreach ($arr as $a){
   $this->SQL = "update client set active=1 where date between ? and ? and type like ?"
   $this->SQL =$this->PDO->prepare($this->SQL);
   $this->SQL->execute(array($this->Since, $this->Until, '%'.$a.'%'));
}

it is only necessary to pass the "%" in the parameter array.

Thanks to those who contributed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.