I have a list
$list = array('a','b','c','d','e','f');
$filter_list = join(", " $list);
select * from test where id_col=12 and try_col in ($filter_list);
Anybody could tell me how Can I do this.?
I have a list
$list = array('a','b','c','d','e','f');
$filter_list = join(", " $list);
select * from test where id_col=12 and try_col in ($filter_list);
Anybody could tell me how Can I do this.?
$list = array('a','b','c','d','e','f');
$filter_list = "'" . join("', '", $list) . "'";
$query = "select * from test where id=12 and try_col in ($filter_list)";
// select * from test where id=12 and try_col in ('a', 'b', 'c', 'd', 'e', 'f')
Note that this will fail if your array values contain '. Here is a workaround:
$list = array("a'a",'a\a','b','c','d','e','f');
$temp = array_map("addslashes", $list);
$query = "SELECT * FROM test WHERE id=12 AND try_col in ('" . implode("','", $temp) . "')";
// SELECT * FROM test WHERE id=12 AND try_col in ('a\'a','a\\a','b','c','d','e','f')
addslashes should be quoted as a string. Also, its much better to use his database-specific escape function. There are several ways to exploit SQL injection when strings are quoted with addslashes and its not locale-aware. Also, the correct way to escape some characters could be different with different RDBMS. That's why I asked him about his database and the API he uses to talk with it.pg_ functions?