I have the following string...
1,2,3,4,5,6
I want to have this like
'1','2','3','4','5','6'
To use in MySQL's WHERE IN ()
Would anyone know the best way to do this?
Thanks!
I have the following string...
1,2,3,4,5,6
I want to have this like
'1','2','3','4','5','6'
To use in MySQL's WHERE IN ()
Would anyone know the best way to do this?
Thanks!
$str = "1,2,3,4,5,6";
echo "'" . implode("','", explode(',', $str)) . "'";
//output: '1','2','3','4','5','6'
You could explode
your string into an array, and then join
(or implode
, it's an alias!) it with the quotes:
$str = '1,2,3,4,5,6';
$arr = explode(",", $str); // turns your string into array(1, 2, 3, 4, 5, 6);
$joined_arr = join("', '", $arr); // becomes 1', '2', '3', '4', '5', '6
$query = "... WHERE ... IN ('$joined_arr')"; // note the two missing quotes have been added in