开发者

Prepping comma delimited string for mysql's WHERE IN

开发者 https://www.devze.com 2023-01-14 10:29 出处:网络
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 ()

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 t开发者_高级运维o do this?

Thanks!


Use explode and implode:

 $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


WHERE (SUBSTRING(SUBSTRING_INDEX(cocodes,cash_out_code,-1),1,1)=','OR...

its actually a combination of string functions.. just try in that direction

0

精彩评论

暂无评论...
验证码 换一张
取 消