简体   繁体   中英

How to make postgresql query by passing php array of strings, tricking with double-quotes?

I am passing array to postgresql in "WHERE" clause. but php is returning strings with double-quotes that postgrsql uses to define column. So postgresql is using the strings values as columns.

$allowed_A = \App\NewA::selectRaw("replace(unaccent(trim(name)), ' ', '') as newname")
   ->whereRaw('replace(unaccent(trim(name)), \' \', \'\') IN ("'.implode(",", $allowed_A).'")')->get();

Getting:

Undefined column: 7 ERROR: column "Purchase,Sale,...

How to fix it? thanks

Your expression encloses the values in double quotes. Change it to this:

$A = \App\NewA::selectRaw("replace(unaccent(trim(name)), ' ', '') as newname")
->whereRaw("replace(unaccent(trim(name)), ' ', '') IN ('".implode("','", $allowed_A)."')")->get();

Pls. bear in mind that your approach is prone to sql injection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM