im trying to use array for mysql where in clause
$result= $myDB->query(\"SELECT sum(total) as total FROM \".$myDB->prefix(\"mydata\").\" WHERE categoryname
the naive solution will be:
$array = ['Cat1', 'Cat2', 'Cat3'];
echo "'" . implode("','", $array) . "'";
but it could introduce sql injection, so you need properly escape data in array first
sample one-line with escaping:
echo "'" . implode("','", array_map('mysql_escape_string', $array)) . "'";
note: mysql_*
functions are deprecated, you need to use mysqli_*
which require connection link
I did something similar a while ago using array_map
, hope it helps:
$args = array_map(function($a) {
return sprintf("'%s'", $a);
}, $args);
$args = join(",", $args);
The above code will iterate over the array and modifies every single element to surround it with ''
. Finally, I join the array with ,
.