php mysql查询字符串数组

时间:2010-04-14 13:35:06

标签: php mysql arrays string

我正在构建一个我在mysql db中检查的字符串。

eg:
formFields[] is an array - input1 is: string1
array_push(strings, formFields)
1st string and mysql query looks like this:
"select * from my table where id in (strings)"

formFields[] is an array - input2 is: string1, string2
array_push(strings, formFields)
2nd string and mysql query looks like this:
"select * from my table where id in (strings)"

formFields[] is an array - input3 is: string1, string2,string3
array_push(strings, formFields)
3rd string and mysql query looks like this:
"select * from my table where id in (strings)"

我想在数组中添加单引号和逗号,以便我为数组字符串添加: “select * from my table where id in('string1','string2','string3')”

我尝试使用数组内爆,但仍然没有运气任何想法?感谢

3 个答案:

答案 0 :(得分:7)

'SELECT ... WHERE id IN ("' . implode('","', $strings) . '")';

答案 1 :(得分:1)

implode()是解决方案,但您不应忘记转义数据:

$data = array(...);
array_walk($data, function(&$elem, $key) {
    $elem = mysql_real_escape_string($elem);
});
$sql = 'SELECT ... id IN ("' . implode('", "', $data) . '");';

答案 2 :(得分:0)

implode("','",$array);