Php mysql query string string

I am creating a string which I check in the 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)"

      

I would like to add single quotes and a comma to the array so that I have this for array strings: "select * from my table where id ('string1', 'string2', 'string3')"

I've tried using implode array but still no luck with any ideas? thank

+2


a source to share


3 answers


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

      



+6


a source


implode () is a solution, but you shouldn't forget about data escaping:



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

      

+1


a source


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

      

0


a source







All Articles